Copying File with Files.copy method
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
public class Test {
public static void main(String[] args) throws Exception {
Path newFile = FileSystems.getDefault().getPath("C:/home/docs/newFile.txt");
Path copiedFile = FileSystems.getDefault().getPath(
"C:/home/docs/copiedFile.txt");
Files.createFile(newFile);
System.out.println("File created successfully!");
Files.copy(newFile, copiedFile);
System.out.println("File copied successfully!");
Files.copy(newFile, copiedFile, StandardCopyOption.REPLACE_EXISTING);
}
}
Related examples in the same category