Hi friend's today I'm here with simple code to rename file name using java. Without wasting any time let's get into the process.
1. Open notepad copy and paste the below code and save it with filename rename.java in any location.
2. Goto the notepad location. Press shift button + right click mouse and select open command window here.
3. If you already set the java path to your PC skip this step. Else type java -version in cmd prompt and hit enter.
a. Make note of the version type.
b. Goto C:\Program Files\Java and open your version folder in that bin and press F6 then copy the path which looks like C:\Program Files\Java\jdk1.8.0_72\bin
c. Open new notepad type set path="paste here" now copy everything and paste it in cmd prompt and hit enter.
d. Now path was set.
4. Now compile file by using cmd javac java1.java
5. Now execute the program by using cmd java rename
6. Now you can see a folder name is changed as the name which you given.
1. Temp is the old name of the folder.
2. Folder is the new name.
3. Make sure to use file name based on file format.
1. Open notepad copy and paste the below code and save it with filename rename.java in any location.
2. Goto the notepad location. Press shift button + right click mouse and select open command window here.
3. If you already set the java path to your PC skip this step. Else type java -version in cmd prompt and hit enter.
a. Make note of the version type.
b. Goto C:\Program Files\Java and open your version folder in that bin and press F6 then copy the path which looks like C:\Program Files\Java\jdk1.8.0_72\bin
c. Open new notepad type set path="paste here" now copy everything and paste it in cmd prompt and hit enter.
d. Now path was set.
4. Now compile file by using cmd javac java1.java
5. Now execute the program by using cmd java rename
6. Now you can see a folder name is changed as the name which you given.
Program:
import java.io.File;Note:
class rename{
public static void main(String args[])
{
File oldFile = new File("E:\Temp");
File newFile = new File("E:\Folder");
if(oldFile.renameTo(newFile)){
System.out.println("Old file name changed");
}else{
System.out.println("Old file name not changed");
}
}
}
1. Temp is the old name of the folder.
2. Folder is the new name.
3. Make sure to use file name based on file format.