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

DeeKay October 3, 2011 at 11:06 pm · java.io

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

<br />
/****************************************************************************************<br />
 * Created on 10-2011 Copyright(c) https://kodehelp.com All Rights Reserved.<br />
 ****************************************************************************************/<br />
package com.kodehelp.javaio;</p>
<p>import java.io.File;</p>
<p>/**<br />
 * Created by https://kodehelp.com<br />
 * Date: 10/3/11<br />
 */<br />
public class RenameFile {<br />
    public static void main(String args[]){<br />
        File file = new File("/conf.txt");</p>
<p>        if (!file.exists() || file.isDirectory()) {<br />
            System.out.println("File does not exist: " + file);<br />
            return;<br />
        }</p>
<p>        File newFile = new File("/conf.ini");</p>
<p>        //Rename<br />
        if (file.renameTo(newFile)) {<br />
            System.out.println("File has been renamed.");<br />
        } else {<br />
            System.out.println("Error renmaing file");<br />
        }<br />
    }<br />
}<br />