Java rename directory example

This example will show you the way to rename any folder in Java.

import java.io.File;
import java.util.Scanner;

/*
 * Here we will learn to rename a Directory.
 */
public class RenameDir {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter the path of directory to rename.");
		String dirPath = scanner.nextLine();
		File dir = new File(dirPath);
		if (!dir.isDirectory()) {
			System.err.println("There is no directory @ given path");
			System.exit(0);
		}

		System.out
				.println("Enter new name of directory(Only Name and Not Path).");
		String newDirName = scanner.nextLine();

		File newDir = new File(dir.getParent() + "\" + newDirName);
		dir.renameTo(newDir);

		System.out.println("Done");
	}
}

 

By | 2017-07-22T23:08:48+00:00 December 15th, 2012|Code Base|1 Comment

About the Author:

One Comment

  1. dumpkick April 2, 2017 at 10:20 am - Reply

    IncreԀible գᥙest there. What occurred after? Good luck!

Leave A Comment