【发布时间】:2012-05-10 09:59:46
【问题描述】:
我们在尝试将文件从一个目录移动到另一个目录时出错的程序。经过多次调试后,我通过编写一个小型实用程序来定位错误,该程序只是将文件从一个目录移动到另一个目录(下面的代码)。事实证明,虽然在本地文件系统上移动文件可以正常工作,但尝试将文件移动到另一个文件系统会失败。
这是为什么?问题可能是特定于平台的——如果这很重要,我们正在 ext3 上运行 Linux。
还有第二个问题;我应该使用 File 类的 renameTo() 方法以外的其他方法吗?似乎这只适用于本地文件系统。
测试(以 root 身份运行):
touch /tmp/test/afile
java FileMover /tmp/test/afile /root/
The file move was successful
touch /tmp/test/afile
java FileMover /tmp/test/afile /some_other_disk/
The file move was erroneous
代码:
import java.io.File;
public class FileMover {
public static void main(String arguments[] ) throws Exception {
boolean success;
File file = new File(arguments[0]);
File destinationDir = new File(arguments[1]);
File destinationFile = new File(destinationDir,file.getName() );
success = file.renameTo(destinationFile);
System.out.println("The file move was " + (success?"successful":"erroneous"));
}
}
【问题讨论】:
标签: java linux filesystems