我已經用getRuntime()API編寫了一個小型java代碼來將文件從一個目錄複製到另一個目錄,但它失敗了,我無法理解爲什麼?當我從shell中運行命令它運行良好,任何人都可以,請讓我知道我現在做的錯誤從Java程序Linux複製文件
private static void copyFilesLinux(String strSource, String strDestination) {
String s;
Process p;
try {
// cp -R "/tmp/S1/"* "/tmp/D1/"
p = Runtime.getRuntime().exec(
"cp -R '" + strSource + "/'* '" + strDestination + "/'");
System.out.println("cp -R \"" + strSource + "/\"* \"" + strDestination + "/\"");
System.out.println("cp -R '" + strSource + "/'* '" + strDestination + "/'");
System.out.println(p.toString());
BufferedReader br = new BufferedReader(new InputStreamReader(
p.getInputStream()));
while ((s = br.readLine()) != null)
System.out.println("line: " + s);
p.waitFor();
System.out.println("exit: " + p.exitValue());
p.destroy();
}
catch (InterruptedException iex) {
iex.printStackTrace();
}
catch (IOException iox) {
iox.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
輸出:
cp -R "/tmp/S1/"* "/tmp/D1/"
cp -R '/tmp/S1/'* '/tmp/D1/'
[email protected]
exit: 1
不應該在引號內?像:'cp -R「/ tmp/S1/*」「/ tmp/D1 /」' – Ash 2013-03-07 07:45:48
@Ash:在這種情況下,它沒有區別 - 通配符不會在Runtime.exec()中擴展。然而,在shell提示符下,將'*'放在引號內可以防止通過globbing擴展 - 這通常不是人們想要的... – thkala 2013-03-07 07:58:37
這是我得到的錯誤cp:can stat'/ tmp/S1/* ':沒有這樣的文件或目錄 – Sandeep540 2013-03-07 07:59:24