Java Rename File – Jave Move File

Filed Under: Java

Java Rename file or move file is a common IO operation. We can use File.renameTo(File dest) method for java rename file and java move file operations.

Java Rename File

java rename file, java move file

File renameTo method returns true if file rename is successful, else it returns false.

Some of the rename operation behaviors are platform dependent. For example, it might fail if you move a file from one filesystem to another or if a file already exists with the same name at the destination directory.

In Mac OS, if the destination file already exists renameTo() override the existing file with the source file.

Java Move File

Moving a file is achieved by the renameTo() function itself. Moving a file doesn’t change file contents, just its directory location. A file complete path contains directory information with the file name, renameTo method is able to change the directory path too, hence move the file.

Java Rename File Example

Here is the example program showing different ways for java rename file or java move file.

RenameFileJava.java


package com.journaldev.files;

import java.io.File;

public class RenameFileJava {

    /**
     * Rename File  or Move File in Java example
     * @param args
     */
    public static void main(String[] args) {
        //absolute path rename file
        File file = new File("/Users/pankaj/java.txt");
        File newFile = new File("/Users/pankaj/java1.txt");
        if(file.renameTo(newFile)){
            System.out.println("File rename success");;
        }else{
            System.out.println("File rename failed");
        }
        
        //relative path rename file
        file = new File("DB.properties");
        newFile = new File("DB_New.properties");
        if(file.renameTo(newFile)){
            System.out.println("File rename success");;
        }else{
            System.out.println("File rename failed");
        }
        
        //java move file from one directory to another
        file = new File("/Users/pankaj/DB.properties");
        newFile = new File("DB_Move.properties");
        if(file.renameTo(newFile)){
            System.out.println("File move success");;
        }else{
            System.out.println("File move failed");
        }
        
        //when source file is not present
        file = new File("/Users/pankaj/xyz.txt");
        newFile = new File("xyz.properties");
        if(file.renameTo(newFile)){
            System.out.println("File move success");;
        }else{
            System.out.println("File move failed");
        }
        
        // when destination already have a file with same name
        file = new File("/Users/pankaj/export.sql");
        newFile = new File("/Users/pankaj/java1.txt");
        if(file.renameTo(newFile)){
            System.out.println("File move success");;
        }else{
            System.out.println("File move failed");
        }
    }

}

We should always check the renameTo return value to make sure rename file is successful because it’s platform dependent and it doesn’t throw IO exception if rename fails. That’s all for a quick java rename file and move file example.

You can checkout more core java examples from our GitHub Repository.

Reference: API Doc

Comments

  1. Alina Valker says:

    Try long path tool. It can delete, copy, move and bulk rename long path files or locked java file.

  2. John says:

    Hi.
    I am very new to java and am having a big problem with the replace() method in java.lang.String.

    Can you suggest a better way. renameTo() does not work on windows 10.

    This call to allIsGood() returns the corrected “oldName”
    public static void main(String[] args) {
    String oldName = “now is the time”;
    String checker = “is”;
    oldName = allIsGood( oldName, checker);
    System.out.println(oldName);

    This call to allIsGood() returns the error className “java/util/regex/Pattern$SliceS”
    File[] files=dirPath.listFiles();
    for (File file : files) {
    if(file.isDirectory()){
    showFileAndDirectory(file);
    }
    if(file.isFile()){
    oldName = file.getName();
    newName = oldName;
    newName = allIsGood( newName, “edit” );
    }
    private static String allIsGood( String toChange, String checker){
    String changed = toChange;
    if( toChange.contains( checker)){
    changed = toChange.replace(checker, “was” );
    System.out.println(changed);
    }
    return changed;
    }
    I have tried using replaceAll() with the same result.
    I am importing the following:
    import java.io.IOException;
    import java.io.File;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;

    If my reading is correct, replace( charset, charset ) and replaceAll( String, String ) are virtually equivalent and both are methods of java.lang.String yet the second call is referred to java.util.regex

  3. Bibekananda Panda says:

    Nice Block

  4. rohanlad23@gmail.com says:

    renaming file- adding version info in filename before extension?

  5. ravi says:

    how to rename a file with a string value

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages