How to rename file in java? How to change file name in java?

Below java code shows how to rename a file or change a file name

/****************************************************************************************
* Created on 10-2011 Copyright(c) https://kodehelp.com All Rights Reserved.
****************************************************************************************/
package com.kodehelp.javaio;

import java.io.File;

/**
* Created by https://kodehelp.com
* Date: 10/3/11
*/
public class RenameFile {
public static void main(String args[]){
File file = new File("/conf.txt");

if (!file.exists() || file.isDirectory()) {
System.out.println("File does not exist: " + file);
return;
}

File newFile = new File("/conf.ini");

//Rename
if (file.renameTo(newFile)) {
System.out.println("File has been renamed.");
} else {
System.out.println("Error renmaing file");
}
}
}

Please Post Your Comments & Reviews

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.