在java中将文件从一个文件夹复制到另一个文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21256219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy file from one folder to another in java
提问by Atal Shrivastava
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?
但它不会将所需的文件复制到其位置。需要什么,它从E盘复制chart.js并复制到newPath变量位置。有没有其他方法可以将文件从一个地方复制到另一个地方?
采纳答案by Evgeniy Dorofeev
You can use standard java.nio.file.Files.copy(Path source, Path target, CopyOption... options)
您可以使用标准 java.nio.file.Files.copy(Path source, Path target, CopyOption... options)
回答by subash
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 src = new File ("E:/QA/chart.js");
假设src
文件存在一个。然后你像这样创建一个新的目标文件
File dest = new File(newPath);
if(!dest.exists())
dest.createNewFile();
Then you can copy
然后你可以复制
FileUtils.copyFile(src,dest);
回答by ravibagul91
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);