我们的一个程序在尝试将文件从一个目录移动到另一个目录时出现了错误。经过长时间的调试,我编写了一个小实用程序来移动文件(下面是代码),最终定位到了错误。原来,在本地文件系统上移动文件很好用,但是尝试将文件移动到另一个文件系统会失败。
为什么会这样呢?这个问题可能与平台有关 - 我们在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"));
}
}