我已经用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