java 无法使用 FileUtils 复制文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41168705/
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
Unable to copy files using FileUtils
提问by Maddy
I am trying to copy the files from one destination to another. I am unable to understand why the error occurs. Any help is appreciated.
我正在尝试将文件从一个目的地复制到另一个目的地。我无法理解为什么会发生错误。任何帮助表示赞赏。
public class FileSearch {
public void findFiles(File root) throws IOException {
File[] listOfFiles = root.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
String iName = listOfFiles[i].getName();
if (listOfFiles[i].isFile() && iName.endsWith(".tif")) {
long fileSize = listOfFiles[i].length();
long sizeToKb = fileSize/1024;
File copyDest = new File("C:\Users\username\Desktop\ZipFiles");
if (fileSize <= 600000) {
System.out.println("|" + listOfFiles[i].getName().toString() + " | Size: " + sizeToKb+" KB");
FileUtils.copyFile(listOfFiles[i], copyDest);
}
} else if (listOfFiles[i].isDirectory()) {
findFiles(listOfFiles[i]);
}
}
}
I get the following error Exception in thread "main" java.io.IOException: Destination 'C:\Users\username\Desktop\ZipFiles' exists but is a directory
我收到以下错误 Exception in thread "main" java.io.IOException: Destination 'C:\Users\username\Desktop\ZipFiles' exists but is a directory
回答by Haifeng Zhang
File srcFile = new File("/path/to/src/file.txt"); // path + filename
File destDir = new File("/path/to/dest/directory"); // path only
FileUtils.copyFileToDirectory(srcFile, destDir);
Try copyFileToDirectory(srcFile, destDir)
, you have to provide the source file absolute path with the file name, and the absolute path to the destination directory.
尝试copyFileToDirectory(srcFile, destDir)
,您必须提供带有文件名的源文件绝对路径,以及目标目录的绝对路径。
In addition, make sure you have write permission to copy the file to the destination. I am always on Linux system don't know how to achieve that, similarly you should be have Administrator privilege on Windows or some similar roles which is able to write files.
此外,请确保您具有将文件复制到目的地的写入权限。我总是在 Linux 系统上不知道如何实现这一点,同样你应该在 Windows 上拥有管理员权限或一些能够写入文件的类似角色。
回答by AJNeufeld
You want FileUtils.copyFileToDirectory(srcFile, destDir)
你要 FileUtils.copyFileToDirectory(srcFile, destDir)
Why does the error occur? FileUtils.copyFileis used to copy a file to a new location. From the documentation:
为什么会出现错误? FileUtils.copyFile用于将文件复制到新位置。从文档:
This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.
此方法将指定源文件的内容复制到指定的目标文件。如果目标文件不存在,则创建保存目标文件的目录。如果目标文件存在,则此方法将覆盖它。
Here, the destination exists, but is not a file; rather it is a directory. You cannot overwrite a directorywith the contents of a file.
这里,目的地存在,但不是文件;而是一个目录。您不能用文件的内容覆盖目录。