【发布时间】:2021-11-12 10:30:09
【问题描述】:
我正在自动化一个特定的过程,在其中一个步骤中,我需要将.gitignore 文件从一个目录复制到另一个目录。
我正在使用 Apache 的 FileUtils 类来实现相同的目的,但是它无法识别这个特定的文件(它虽然存在于文件夹中)。该代码适用于其他文件。
这是我的代码:
public void copyFile(String destinationPath, String file) throws IOException {
ClassPathResource classPathResourceAPIUtils = new ClassPathResource(file);
String fileName = file.substring(file.lastIndexOf("/"));
InputStream inputStreamapiUtils = classPathResourceAPIUtils.getInputStream();
BufferedReader readUtils = new BufferedReader(new InputStreamReader(inputStreamapiUtils));
List<String> utilsLines = readUtils.lines().collect(Collectors.toList());
FileUtils.writeLines(new File(destinationPath+fileName), utilsLines, false);
}
【问题讨论】:
-
当您实际上可以移动文件时,您为什么还要读写文件? docs.oracle.com/javase/tutorial/essential/io/move.html 点文件通常是“隐藏的”,但我认为 Java 并不真正关心这一点,所以这里似乎有其他问题
-
我的错。我的意思是我试图“复制”它,而不是“移动”它。编辑了问题。
-
您也可以在不读取文件内容的情况下复制文件docs.oracle.com/javase/tutorial/essential/io/copy.html
标签: java file apache-commons