How to rename or move a file in Java

In Java, we can use the NIO Files.move(source, target) to rename or move a file. import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; //… Path source = Paths.get("/home/mkyong/newfolder/test1.txt"); Path target = Paths.get("/home/mkyong/newfolder/test2.txt"); try{ Files.move(source, target); } catch (IOException e) { e.printStackTrace(); } 1. Rename a file in the same directory. 1.1 This example renames a …

Read more