Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while copying files from source to destination java

Tags:

People also ask

How do I copy and paste a file in Java?

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.

How do I move a file from one directory to another in Java 8?

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.

How do you copy a file from one folder to another?

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