简体   繁体   English

用Java将文件从一个文件夹复制到另一个文件夹

[英]Copy file from one folder to another in Java

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.需要什么,它从E盘复制chart.js并复制到newPath变量位置。

Is there some other way to copy files from one place to another?有没有其他方法可以将文件从一个地方复制到另一个地方?

您可以使用标准java.nio.file.Files.copy(Path source, Path target, CopyOption... options)

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);

Try this.尝试这个。

FileUtils.copyFile(src, dest)

this is happening in copy.这是在复制中发生的。 so this point of view File src = new File ("E:/QA/chart.js");所以这个观点File src = new File ("E:/QA/chart.js"); assume src file existing one.假设src文件存在一个。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM