Folder, file renaming [Java] Rename a file or folder

[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:

 

[java]  view plain copy  
 
  1. import java.io. *;  
  2. public class renameTest {  
  3.   
  4.     public static void main(String[] args) {  
  5.         //The path of the original file you want to name  
  6.         File file = new File("f:/a/a.xlsx");  
  7.         //Change the original file to f:\a\b.xlsx, where the path is necessary. Notice  
  8.         file.renameTo(new File("f:/a/b.xlsx"));  
  9.         //The path of the original folder that you want to name  
  10.         File file1 = new File("f:/A");  
  11.         //Change the original folder to A, where the path is necessary. Notice  
  12.         file1.renameTo(new File("f:/B"));  
  13.     }  
  14.   
  15. }  

 

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:

 

[java]  view plain copy  
 
  1. import java.io. *;  
  2. public class renameTest {  
  3.   
  4.     public static void main(String[] args) {  
  5.         //Rename the original file f:/a/a.xlsx to f:/a/b.xlsx, where the path is necessary. Notice  
  6.         new File("f:/a/a.xlsx").renameTo(new File("f:/a/b.xlsx"));  
  7.     }  
  8.   
  9. }  

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 :

 

 

[java]  view plain copy  
 
  1. import java.io. *;  
  2. public class renameTest {  
  3.   
  4.     public static void main(String[] args) {  
  5.         new File("f:/a/a.xlsx").renameTo(new File("c:/a/b.bmp"));  
  6.     }  
  7.   
  8. }  

 

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:

 

[java]  view plain copy  
 
  1. import java.io. *;  
  2. public class renameTest {  
  3.   
  4.     public static void main(String[] args) {  
  5.         //The path of the original file you want to name  
  6.         File file = new File("f:/a/a.xlsx");  
  7.         //Change the original file to f:\a\b.xlsx, where the path is necessary. Notice  
  8.         file.renameTo(new File("f:/a/b.xlsx"));  
  9.         //The path of the original folder that you want to name  
  10.         File file1 = new File("f:/A");  
  11.         //Change the original folder to A, where the path is necessary. Notice  
  12.         file1.renameTo(new File("f:/B"));  
  13.     }  
  14.   
  15. }  

 

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:

 

[java]  view plain copy  
 
  1. import java.io. *;  
  2. public class renameTest {  
  3.   
  4.     public static void main(String[] args) {  
  5.         //Rename the original file f:/a/a.xlsx to f:/a/b.xlsx, where the path is necessary. Notice  
  6.         new File("f:/a/a.xlsx").renameTo(new File("f:/a/b.xlsx"));  
  7.     }  
  8.   
  9. }  

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 :

 

 

[java]  view plain copy  
 
  1. import java.io. *;  
  2. public class renameTest {  
  3.   
  4.     public static void main(String[] args) {  
  5.         new File("f:/a/a.xlsx").renameTo(new File("c:/a/b.bmp"));  
  6.     }  
  7.   
  8. }  

 

After execution, the renameTo method returns false, and then the system's folder does not change.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325024494&siteId=291194637