以前我一直以為File#renameTo(File)方法與OS下面的 move/mv 命令是相同的,可以達到改名、移動文件的目的。不過后來經(jīng)常發(fā)現(xiàn)問題,真的很bt,File#renameTo(File)方法會返回失敗(false),文件沒有移動,又查不出原因,再后來干脆棄用該方法,自己實現(xiàn)一個copy方法,問題倒是再也沒有出現(xiàn)過。
昨天老板同學又遇到這個問題,F(xiàn)ile#renameTo(File)方法在windows下面工作的好好的,在linux下偶爾又失靈了?;氐郊椅覓吡艘槐镴DK中File#renameTo(File)方法的源代碼,發(fā)現(xiàn)它調(diào)用的是一個本地的方法(native method),無法再跟蹤下去。網(wǎng)上有人說該方法在window下是正常的,在linux下面是不正常的。這個很難說通,SUN不可能搞出這種平臺不一致的代碼出來啊。
后面在SUN的官方論壇上看到有人提到這個問題“works on windows, don't work on linux”,后面有人回復說是“file systems”不一樣。究竟怎么不一樣呢?還是沒有想出來...
后面在一個論壇里面發(fā)現(xiàn)了某人關于這個問題的闡述:
In the Unix'esque O/S's you cannot renameTo() across file systems. This behavior is different than the Unix "mv" command. When crossing file systems mv does a copy and delete which is what you'll have to do if this is the case.
The same thing would happen on Windows if you tried to renameTo a different drive, i.e. C: -> D:
終于明白咯。
做個實驗:
注意看結果,從C盤到E盤失敗了,從C盤到D盤成功了。因為我的電腦C、D兩個盤是NTFS格式的,而E盤是FAT32格式的。所以從C到E就是上面文章所說的"file systems"不一樣。從C到D由于同是NTFS分區(qū),所以不存在這個問題,當然就成功了。
果然是不能把File#renameTo(File)當作move方法使用。
可以考慮使用apache組織的commons-io包里面的FileUtils#copyFile(File,File)和FileUtils#copyFileToDirectory(File,File)方法實現(xiàn)copy的效果。至于刪除嘛,我想如果要求不是那么精確,可以調(diào)用File#deleteOnExit()方法,在虛擬機終止的時候,刪除掉這個目錄或文件。
BTW:File是文件和目錄路徑名的抽象表示形式,所以有可能是目錄,千萬小心。
下面我寫的一個實現(xiàn)方法
/**
* 使用FileChannel拷貝文件
*
* @param srcFile
* @param destFile
* @throws IOException
*/
public static void copyUseChannel(File srcFile, File destFile)
throws IOException {
if ((!srcFile.exists()) || (srcFile.isDirectory())) {
return;
}
if (!destFile.exists()) {
createFile(destFile.getAbsolutePath());
}
FileChannel out = null;
FileChannel in = null;
try {
out = new FileOutputStream(destFile).getChannel();
in = new FileInputStream(srcFile).getChannel();
ByteBuffer buffer = ByteBuffer.allocate(102400);
int position = 0;
int length = 0;
while (true) {
length = in.read(buffer, position);
if (length <= 0) {
break;
}
// System.out.println("after read:"+buffer);
buffer.flip();
// System.out.println("after flip:"+buffer);
out.write(buffer, position);
position += length;
buffer.clear();
// System.out.println("after clear:"+buffer);
}
} finally {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
}
}