java執行shell命令中有空格的處理方法

2021-07-31 08:32:08 字數 348 閱讀 3659

runtime.getruntime().exec(cmdstring);如果此時cmdstring中的引數(例如cp檔案時檔名)含有特殊符號空格,此時就會出現錯誤,因為原始碼會按照一些特殊字元(" \t\n\r\f",注意到其中含有空格)去切分cmdstring變為陣列,所以有空格會導致錯誤,這種情況可以用命令陣列實現,就是把cmdstring裡的東西自己放到陣列裡,就不用原始碼去切分了就不會有問題,例:

abc af.zip為乙個檔名

cmdstring=「cp abc af.zip  dir」這樣是有問題的

string cmdarray=new string("cp","abc af.zip","dir") it is ok

java 通過 ssh 執行 shell 命令

public abstract class shell implements autocloseable class ssh2shell extends shell public static string errormsg new string 利用jsch包實現遠端主機shell命令執行,鏈結遠...

Android執行shell命令

android執行shell命令 一 方法 執行乙個shell命令,並返回字串值 param cmd 命令名稱 引數組成的陣列 例如 param workdirectory 命令執行路徑 例如 system bin return 執行結果組成的字串 throws ioexception public...

python執行shell命令

在此比較一下兩種方法執行系統命令的方法,以方便於日後運用 1.os.system system command exit status execute the command a string in a subshell.僅僅在乙個子終端執行系統命令,而不能獲取命令執行後的返回資訊.os.syste...