April 19, 2024

Renaming a File Using Java Program

This post shows how to rename a file using a Java program. The options you have in Java for renaming a file are as given below-

  1. renameTo(File dest)- You can use renameTo() method of the java.io.File class. See example.
  2. Files.move()- Java 7 onward you can also use Files.move() method to rename a file. See example.

Renaming a file using renameTo() method Java program

renameTo() method renames the file denoted by this abstract pathname.

Note that many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists.

Method returns true if and only if the renaming succeeded; false otherwise. The return value should always be checked to make sure that the rename operation was successful.

In the Java example we’ll cover the scenario where you want to hide a file in Unix system by appending a "." in front of the file name. For doing that you can rename a file to have a new name as "."+fileName.

import java.io.File;

public class RenameFile {
  public static void main(String[] args) {		
    File file = new File("/home/knpcode/Documents/output");
    System.out.println("New Name- " + file.getParent()+"/."+file.getName());
    // renaming file 
    if(file.renameTo(new File(file.getParent()+"/."+file.getName()))) {
      System.out.println("File renamed successfully");
    }else {
      System.out.println("File renaming failed");
    }		
  }
}
Output
New Name- /home/knpcode/Documents/.output
File renamed successfully

Renaming a file using Files.move() method Java program

Files.move() method is used to both move or rename a file to a target file.

Here is a Java example to rename a file, keeping the file in the same directory.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class RenameFile {
  public static void main(String[] args) {	
    // source files		
    Path filePath = Paths.get("/home/knpcode/Documents/output");
    try {
      //renaming file
      Files.move(filePath, filePath.resolveSibling("output_bck"));
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

Note the use of resolveSibling(String other) method of the java.nio.file.Path which converts a given path string to a Path and resolves it against this path's parent path in exactly the manner specified by the resolveSibling method. That makes the source path as the traget path also.

Java example where the file is renamed and also moved from /home/knpcode/Documents/ to /home/knpcode/Documents/Test directory.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class RenameFile {
  public static void main(String[] args) {			
    Path filePath = Paths.get("/home/knpcode/Documents/output");
    Path targetPath = Paths.get("/home/knpcode/Documents/Test/output_bck");
    try {
      Files.move(filePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

That's all for the topic Renaming a File Using Java Program. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment