[Java] Rename a file or folder
In Java, renaming a file or folder is very simple, because Java's File class has encapsulated the renameTo method.
This method is used to modify the name of a file or folder. For example the following program:
- import java.io. *;
- public class renameTest {
- public static void main(String[] args) {
- //The path of the original file you want to name
- File file = new File("f:/a/a.xlsx");
- //Change the original file to f:\a\b.xlsx, where the path is necessary. Notice
- file.renameTo(new File("f:/a/b.xlsx"));
- //The path of the original folder that you want to name
- File file1 = new File("f:/A");
- //Change the original folder to A, where the path is necessary. Notice
- file1.renameTo(new File("f:/B"));
- }
- }
Once run, it will first rename f:/a/a.xlsx to f:/a/b.xlsx, and then rename the folder f:/A to f:/B.
Note that under Windows, folder and file names are not case-sensitive.
So f:/A and f:/a are actually the same thing.
In fact, the folder name can also be written shorter, and even the declaration of the File class is not needed.
Rename f:/a/a.xlsx to f:/a/b.xlsx and write it like this:
- import java.io. *;
- public class renameTest {
- public static void main(String[] args) {
- //Rename the original file f:/a/a.xlsx to f:/a/b.xlsx, where the path is necessary. Notice
- new File("f:/a/a.xlsx").renameTo(new File("f:/a/b.xlsx"));
- }
- }
Then, it is worth noting that the parent path in front of the renamed file must be the same, that is, the following way is wrong :
- import java.io. *;
- public class renameTest {
- public static void main(String[] args) {
- new File("f:/a/a.xlsx").renameTo(new File("c:/a/b.bmp"));
- }
- }
After execution, the renameTo method returns false, and then the system's folder does not change.
In Java, renaming a file or folder is very simple, because Java's File class has encapsulated the renameTo method.
This method is used to modify the name of a file or folder. For example the following program:
- import java.io. *;
- public class renameTest {
- public static void main(String[] args) {
- //The path of the original file you want to name
- File file = new File("f:/a/a.xlsx");
- //Change the original file to f:\a\b.xlsx, where the path is necessary. Notice
- file.renameTo(new File("f:/a/b.xlsx"));
- //The path of the original folder that you want to name
- File file1 = new File("f:/A");
- //Change the original folder to A, where the path is necessary. Notice
- file1.renameTo(new File("f:/B"));
- }
- }
Once run, it will first rename f:/a/a.xlsx to f:/a/b.xlsx, and then rename the folder f:/A to f:/B.
Note that under Windows, folder and file names are not case-sensitive.
So f:/A and f:/a are actually the same thing.
In fact, the folder name can also be written shorter, and even the declaration of the File class is not needed.
Rename f:/a/a.xlsx to f:/a/b.xlsx and write it like this:
- import java.io. *;
- public class renameTest {
- public static void main(String[] args) {
- //Rename the original file f:/a/a.xlsx to f:/a/b.xlsx, where the path is necessary. Notice
- new File("f:/a/a.xlsx").renameTo(new File("f:/a/b.xlsx"));
- }
- }
Then, it is worth noting that the parent path in front of the renamed file must be the same, that is, the following way is wrong :
- import java.io. *;
- public class renameTest {
- public static void main(String[] args) {
- new File("f:/a/a.xlsx").renameTo(new File("c:/a/b.bmp"));
- }
- }
After execution, the renameTo method returns false, and then the system's folder does not change.