How To Rename A File In Java

This little program demonstrate how to rename a file in Java. The code

  • Takes original file name and renamed file name as parameter
  • Checks whether the file exists
  • Checks whether the input file provided is a directory
  • Renames a file if it exists and is not a directory
  • Throws an error if problem occurs renaming the file.

package com.kushal.utils;

/**
 * @author Kushal Paudyal
 * www.sanjaal.com/java
 * Last Modified On 05-04-2009
 *
 * Demonstrate the process of renaming a file in Java
 */

import java.io.File;

public class FileRenameInJava {

	public static void renameFile(String originalFileName,
			String renamedFileName) {

		File originalFile = new File(originalFileName);
		/**
		 * Check if file exists
		 */
		boolean fileExists = originalFile.exists();

		/**
		 * Check if it is a directory
		 */
		boolean isDirectory = originalFile.isDirectory();

		/**
		 * If file does not exist, return
		 */
		if (!fileExists) {

			System.out.println("File does not exist: " + originalFileName);
			System.out.println("Rename Operation Aborted.");
			return;
		}
		/**
		 * If file is a directory, return
		 */
		if (isDirectory) {
			System.out
					.println("The parameter you have provided is a directory: "
							+ originalFileName);
			System.out.println("Rename Operation Aborted.");
			return;
		}

		File renamedFile = new File(renamedFileName);

		boolean renamed = originalFile.renameTo(renamedFile);
		if (renamed) {
			System.out.println("File Has Been Renamed Successfully.");
			System.out.println("Original File Name: " + originalFileName);
			System.out.println("Rename File Name: " + renamedFileName);
		} else {
			System.out
					.println("Error occurred while trying to rename the file.");
		}

	}

	public static void main(String[] args) {
		renameFile("C:/Temp/def.txt", "C:/Temp/abc.txt");
	}
}

———————-
Here is the sample output:

File Has Been Renamed Successfully.
Original File Name: C:/Temp/def.txt
Rename File Name: C:/Temp/abc.txt

[My Video Promotion]
  1. How to read / write UTF8 and Non-UTF8 files in Java
  2. How To Print A Text File In Java
  3. How to read a tab separated or tab delimited file in Java program and print the content to console
  4. Computing the total, free and usable disk space easily using JDK 1.6
  5. Reading the Resource or Property files in Java as a file stream and outputting the content to console
  6. File Copy From Local Folder or Network Folder Using Java With Capability To Limit The Number
  7. Java Tutorial – Using JCIFS to copy files to shared network drive using username and password
  8. Reading / Writing File in Java and String Manipulation
  9. How To Read DOC file Using Java and Apache POI
  10. Writing To Excel File Using Apache POI
  11. Reading Excel File Using Java And Apache POI
  12. Calculating Folder Size In Java
  13. How To Rename A File In Java
  14. Calculating Folder Size Graphically and Generating Directory Size Chart In Java
Tagged , , , , , , , . Bookmark the permalink.

6 Responses to Calculating Folder Size Graphically and Generating Directory Size Chart In Java

  1. nocdib says:

    Thank you for this! It is just what I need

  2. d says:

    would that help you tocreate the file from ubunt server

  3. komal gaikwad says:

    in this code i got problem of array out of bound plz tell me solution.

  4. komal gaikwad says:

    In this code i got error
    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1
    plz send me proper code….

  5. Neethu says:

    Thank you very much for the solution. This is the one which i was searching.

  6. Manikant says:

    I need to copy to the shared folder which is shared and can be authenticate by using credentials.

    but here the code is giving to write .

    I need copy my folder from my local to shared location

    Please help

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.