Renaming a file using Java :
Among the many operations that can be done on a file have a file renaming. This library of Java, Java IO, offers a method on the File class. If we remember a little philosophy class IO Java will see that the File class represents the abstraction of a file or directory, regardless of the file system that we have below. The first thing to do is instantiate the File class on the file you want to do the rename. We use the File class constructor, as we see in the following line of code :
Code:
File f1 = new File ("file1.txt");
The following will instantiate a new class File with the name of the file you want to. Let's look at the line of code :
Code:
File f2 = new File ("file2.txt");
With the two abstractions of files leaves us with the run method. RenameTo (File) on the first file. The method. RenameTo (File) receives as parameters a File object with the new name. Which we have called f2
Code:
boolean right = f1.renameTo (f2);
The method. RenameTo (File) returns a boolean indicating whether it has been making renowned or not. To that end validate if the Boolean variable to provide information to the user.
Code:
if (correct)
System.out.println ("The right has been renamed");
else
System.out.println ("The renowned not been able to do");
Bookmarks