Copy file from one folder to another in java

痞子三分冷 提交于 2019-12-10 15:54:31

问题


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?


回答1:


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




回答2:


You can use this

Path FROM = Paths.get(Your Source file complete path);
Path TO = Paths.get(Destination complete path);
CopyOption[] options = new CopyOption[]{
  StandardCopyOption.REPLACE_EXISTING,
  StandardCopyOption.COPY_ATTRIBUTES
}; 
java.nio.file.Files.copy(FROM, TO, options);



回答3:


Try this.

FileUtils.copyFile(src, dest)

this is happening in copy. so this point of view File src = new File ("E:/QA/chart.js"); assume src file existing one. Then you create a new destination file like this

File dest = new File(newPath);
if(!dest.exists())
  dest.createNewFile();

Then you can copy

FileUtils.copyFile(src,dest);


来源:https://stackoverflow.com/questions/21256219/copy-file-from-one-folder-to-another-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!