请慎用java的File#renameTo(File)方法

来源:互联网 发布:雇网络维护人员要求 编辑:程序博客网 时间:2024/05/17 04:58


不管在什么地方,什么时候,学习是快速提升自己的能力的一种体现!!!!!!!!!!!


以前我一直以为File#renameTo(File)方法与OS下面的 move/mv 命令是相同的,可以达到改名、移动文件的目的。不过后来经常发现问题:File#renameTo(File)方法会返回失败(false),文件没有移动,又查不出原因,再后来干脆弃用该方法,自己实现一个copy方法,问题倒是再也没有出现过。

昨天老板同学又遇到这个问题,File#renameTo(File)方法在windows下面工作的好好的,在Linux下偶尔又失灵了。回到家我扫了一遍JDK中File#renameTo(File)方法的源代码,发现它调用的是一个本地的方法(native method),无法再跟踪下去。网上有人说该方法在window下是正常的,在linux下面是不正常的。这个很难说通,SUN不可能搞出这种平台不一致的代码出来啊。

后面在SUN的官方论坛上看到有人提到这个问题“works on windows, don't work on linux”,后面有人回复说是“file systems”不一样。究竟怎么不一样呢?还是没有想出来...

后面在一个论坛里面发现了某人关于这个问题的阐述:

引用

[plain] view plain copy
  1. In the Unix'esque O/S's you cannot renameTo() across file systems. This behavior is different than the Unix "mv" command.   
  2. When crossing file systems mv does a copy and delete which is what you'll have to do if this is the case.   
  3.   
  4. The same thing would happen on Windows if you tried to renameTo a different drive, i.e. C: -> D:  

终于明白咯。做个实验:
[java] view plain copy
  1. File sourceFile = new File("c:/test.txt");    
  2. File targetFile1 = new File("e:/test.txt");    
  3. File targetFile2 = new File("d:/test.txt");    
  4. System.out.println("source file is exist? " + sourceFile.exists() + ", source file => " + sourceFile);    
  5. System.out.println(targetFile1 + " is exist? " + targetFile1.exists());    
  6. System.out.println("rename to " + targetFile1 + " => " + sourceFile.renameTo(targetFile1));    
  7. System.out.println("source file is exist? " + sourceFile.exists() + ", source file => " + sourceFile);    
  8. System.out.println(targetFile2 + " is exist? " + targetFile2.exists());    
  9. System.out.println("rename to " + targetFile2 + " => " + sourceFile.renameTo(targetFile2));  

结果:
[java] view plain copy
  1. source file is exist? true, source file => c:\test.txt    
  2. e:\test.txt is exist? false    
  3. rename to e:\test.txt => false    
  4. source file is exist? true, source file => c:\test.txt    
  5. d:\test.txt is exist? false    
  6. rename to d:\test.txt => true   

注意:,从C盘到E盘失败了,从C盘到D盘成功了。因为我的电脑C、D两个盘是NTFS格式的,而E盘是FAT32格式的。所以从C到E就是上面文章所说的"file systems"不一样。从C到D由于同是NTFS分区,所以不存在这个问题,当然就成功了。

果然是不能把File#renameTo(File)当作move方法使用。

可以考虑使用apache组织的commons-io包里面的FileUtils#copyFile(File,File)和FileUtils#copyFileToDirectory(File,File)方法实现copy的效果。至于删除嘛,我想如果要求不是那么精确,可以调用File#deleteOnExit()方法,在虚拟机终止的时候,删除掉这个目录或文件。

OK,到此为止都是引用原作者的内容,http://xiaoych.iteye.com/blog/149328,一下是我自己做的实验。

首先在windows上作实验。

[java] view plain copy
  1. import java.io.File;  
  2.   
  3. public class Test {  
  4.   
  5.     public static void main(String[] args) {  
  6.         // TODO Auto-generated method stub  
  7.         System.out.println("Test");  
  8.         File sourceFile = new File("c:/test.txt");  
  9.         File targetFile = new File("d:/test.txt");  
  10.         try {  
  11.             boolean result = targetFile.renameTo(sourceFile);  
  12.             System.out.println("result = " + result);  
  13.         } catch (Exception e) {  
  14.             // TODO Auto-generated catch block  
  15.             e.printStackTrace();  
  16.         }  
  17.     }  
  18.   
  19. }  

结构为true,但是我的C盘的文件系统为FAT32,D盘为NTFS,并不能重现原作者的问题。此时看了原文章的一些评论内容,说可能与系统有关,也有人说可能与JDK版本有关,但是现在我也不能确定具体与什么有关。

接着实验,我又换了一个环境,在Ubuntu上做实验。

[java] view plain copy
  1. package com.darren.test;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5.   
  6. import org.apache.commons.io.FileUtils;  
  7.   
  8. public class Test {  
  9.     public static void main(String[] args) {  
  10.         File source = new File("/test.txt");  
  11.         File target = new File("/dev/test.txt");  
  12.         Test.testCopy(source, target);  
  13.   
  14.     }  
  15.   
  16.     public static void testRenameTo(File source, File target) {  
  17.         try {  
  18.             boolean result = source.renameTo(target);  
  19.             System.out.println(result);  
  20.         } catch (Exception e) {  
  21.             // TODO Auto-generated catch block  
  22.             e.printStackTrace();  
  23.         }  
  24.     }  
  25.   
  26.     public static void testCopy(File source, File target) {  
  27.         try {  
  28.             FileUtils.copyFile(source, target);  
  29.         } catch (IOException e) {  
  30.             // TODO Auto-generated catch block  
  31.             e.printStackTrace();  
  32.         }  
  33.     }  
  34. }  

测试renameTo方法的时候失败了,而测试copy方法的时候成功了,我测试的两个目录的文件系统也不一样。

再做一个测试:

最近遇到一个BUG,说mount一个forder之后,使用renameTo方法失败,然后我就在Ubuntu上做了这个测试,发现确实失败,因为mount之后得到文件系统有变化,导致使用renameTo前后两个目录的文件系统不一致,导致无法移动,所以在此说明renameTo方法不等同于操作系统的move方法,当然可以使用apache的commons-io中的FileUtils.moveFile(source, target)去替换renameTo方法。那么还有没有官方的证据证明renameTo方法存在缺陷呢? 答案是当然有,让我门看一下JDK的API文档:

[java] view plain copy
  1. public boolean renameTo(File dest)  
  2. Renames the file denoted by this abstract pathname.  
  3. Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to   
  4. move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination   
  5. abstract pathname already exists. The return value should always be checked to make sure that the rename operation   
  6. was successful.  
  7.   
  8. Note that the Files class defines the move method to move or rename a file in a platform independent manner.  
  9.   
  10. Parameters:  
  11. dest - The new abstract pathname for the named file  
  12. Returns:  
  13. true if and only if the renaming succeeded; false otherwise  
  14. Throws:  
  15. SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write   
  16. access to either the old or new pathnames  
  17. NullPointerException - If parameter dest is null  

所以说,当遇到多文件系统的时候,请尽量避免使用File#renameTo(File)方法,可是使用apache的commons-io包去替换,避免一些潜在BUG。
原创粉丝点击