我用 getRuntime() API 编写了一个小的 java 代码,将文件从一个目录复制到另一个目录,但它失败了,我不明白为什么?当我从 shell 运行命令时,它运行良好,谁能告诉我我正在做的错误
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/'
java.lang.UNIXProcess@525483cd
exit: 1