Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a file using java.io packages?

Tags:

java

file-io

How to rename a file using java.io packages?

like image 988
firsttime Avatar asked Dec 03 '22 08:12

firsttime


2 Answers

File oldfile = new File(old_name);
File newfile = new File(new_name);
boolean Rename = oldfile.renameTo(newfile);

The boolean Rename will be true if it successfully renamed the oldfile.

like image 136
A B Avatar answered Dec 06 '22 09:12

A B


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"));
      }
    }

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

like image 23
Andrei Sfat Avatar answered Dec 06 '22 10:12

Andrei Sfat