Copy file from one folder to another in Java

Atal Shrivastava picture Atal Shrivastava · Jan 21, 2014 · Viewed 32.5k times · Source

I am trying to copy a file from one folder to another folder.

Here's what I have got in my code:

public static void copyFile(String path) throws IOException{
   newPath = path;    
   File destination = new File ("E:/QA/chart.js"); 
   FileUtils.copyFile(destination, new File(newPath));      
}

But it is not copying the desired file to its location. What is required, its copy chart.js from E drive and copy to the newPath variable location.

Is there some other way to copy files from one place to another?

Answer

Evgeniy Dorofeev picture Evgeniy Dorofeev · Jan 21, 2014

You can use standard java.nio.file.Files.copy(Path source, Path target, CopyOption... options)