How to Rename a Folder in Java


In this example we will show how to rename a folder in Java.

Source Code

package com.beginner.examples;

import java.io.*;

public class FileOperator13Example { 
	public static void main(String[] args )
    {	
		try
        {
        	 File path = new File("example");
        	 if (!path.exists()) 
        	 {
        		 path.createNewFile();
        	 }
        	 
        	 File newName = new File("test");
        	 path.renameTo(newName);
        	 System.out.println("Folder '"+path+"' is renamed to '"+newName+"'.");
        } 
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

Output:

Folder 'example' is renamed to 'test'.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments