Another common way to copy a file with Java is by using the commons-io library. The latest version can be downloaded from Maven Central. Then, to copy a file we just need to use the copyFile() method defined in the FileUtils class. The method takes a source and a target file.
You can move a file or directory by using the move(Path, Path, CopyOption...) method. The move fails if the target file exists, unless the REPLACE_EXISTING option is specified.
Right-click the file or folder you want, and from the menu that displays click Move or Copy. The Move or Copy window opens. Scroll down if necessary to find the destination folder you want.
//original file
Path original = Paths.get("C:\\Users\\Laksahan\Desktop\\bg.jpg");
File f = new File("C:\\Users\\Laksahan\\Desktop\\bg.jpg");
// new file
Path destination = Paths.get("C:\\Program Files\\Tour v0.1\\image\\"+f.getName());
try {
Files.copy(original, destination, LinkOption.NOFOLLOW_LINKS);
} catch (IOException x) {
x.printStackTrace();
}
i tried above method to copy files, it wont work and it prints this error
java.nio.file.NoSuchFileException: C:\Users\Laksahan\Desktop\bg.jpg -> C:\Program Files\Tour v0.1\image\bg.jpg
please help
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With