Rename a File - Java File Path IO

Java examples for File Path IO:File Rename

Description

Rename a File

Demo Code

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

public class Main {
  public static void main(String[] args) throws Exception {
    Path movefrom = FileSystems.getDefault().getPath(
        "C:/folder1/photos/test.jpg");

    try {//from   w  ww .java  2 s.  c om
      Files.move(movefrom, movefrom.resolveSibling("test_renamed.jpg"),
          StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
      System.err.println(e);
    }

  }
}

Result


Related Tutorials