我正在尝试使用Java运行时类压缩和存档文件夹中的所有文件。我的代码片段看起来就象这样:使用Java运行时压缩和存档文件夹中的文件
public static void compressFileRuntime() throws IOException, InterruptedException {
String date = Util.getDateAsString("yyyy-MM-dd");
Runtime rt = Runtime.getRuntime();
String archivedFile = "myuserData"+date+".tar.bz2";
String command = "tar --remove-files -cjvf "+archivedFile+" marketData*";
File f = new File("/home/amit/Documents/");
Process pr = rt.exec(command, null, f);
System.out.println("Exit value: "+pr.exitValue());
}
上面的代码不存档和压缩文件如预期,虽然它创建文件夹“/home/amit/Documents/
”在文件myuserData2009-11-18.tar.bz2
。
另外的输出是
Exit value: 2.
虽然我是否来自命令行执行相同的命令,它给出了预期的结果。
请告诉我我失踪了。
感谢
阿米特
感谢rsp。 但该文件夹包含许多其他类型的文件,我只需要存档以marketData开头的文件。此外,文件通过FTP转储到该文件夹中,文件数量也不相同。所以使用你指定的第一种或第三种方法似乎很难。 我会尝试第二个。你能否详细说明一下。 谢谢, – Amit 2009-11-18 11:16:42
它的工作 谢谢 – Amit 2009-11-18 12:10:32