如何使用java.io包重命名文件?

fir*_*ime 4 java file-io

如何使用java.io包重命名文件?

A B*_*A B 8

File oldfile = new File(old_name);
File newfile = new File(new_name);
boolean Rename = oldfile.renameTo(newfile);
Run Code Online (Sandbox Code Playgroud)

Rename如果成功重命名旧文件,则布尔值为true.


And*_*fat 6

import java.io.File;
import java.io.IOException
    public class Rename {
      public static void main(String[] argv) throws IOException {

        // Construct the file object. Does NOT create a file on disk!
        File f = new File("Rename.java~"); // backup of this source file.

        // Rename the backup file to "junk.dat"
        // Renaming requires a File object for the target.
        f.renameTo(new File("junk.dat"));
      }
    }
Run Code Online (Sandbox Code Playgroud)

参考:http://www.java2s.com/Code/Java/File-Input-Output/RenameafileinJava.htm