renameTo
public boolean renameTo(File dest)
Renames the file denoted by this abstract pathname. 
Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful. 
Parameters:
dest - The new abstract pathname for the named file 
Returns:
true if and only if the renaming succeeded; false otherwise 
Throws: 
SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write access to either the old or new pathnames 
NullPointerException - If parameter dest is null

解决方案 »

  1.   

    i don't know and i want not know
      

  2.   

    这个我也查了,但有没有example?谢谢!!!
      

  3.   

    直接用file的rrenameTo(File) 方法不就行了或麻烦一点,copy一份出来,将原文件删除(这个不推荐使用)
      

  4.   

    要例子可以看看下面的,我试过了
    import java.io.*;public class RenameFile
    {
    public static void main(String[] args)
    {
    File file = new File("c:/test.dso");
    file.renameTo(new File("c:/tt.xml"));
    }
    }
      

  5.   

    File f = new File("d:/temp/aaaa.txt");
    File f2 = new File("d:/temp/aaaa222.txt");
    boolean renameFlag = f.renameTo(f2);
    System.out.println(renameFlag);
    把f改名成f2. renameFlag = true说明成功,false说明失败
      

  6.   

    import java.io.*;public class RenameFile
    {
    public static void main(String[] args)
    {
                      try
                      {
                           (new File("c:/a.xml")).renameTo(new File("c:/b.xml"));
                      }
                      catch(Exception e){System.out.println(e.getMessage());}
    }
    }