猿问

如何使用Java将文本附加到一个目录中的多个文件

我有很多包含一些数据的 .txt 文件

1.txt
2.txt
3.txt
...

我想添加相同的文本,例如"hello world"添加到同一目录中的每个 txt 文件。

我知道在那种情况下如何处理一个文件,但如何处理多个文件呢?我必须使用 Java 来做到这一点......


梵蒂冈之花
浏览 126回答 3
3回答

料青山看我应如是

您可以java.nio结合使用 Java 8 功能来列出文件并为每个文件执行一些操作。有一种方法可以将文本附加到文件。请参阅此示例并阅读代码中的一些注释:public static void main(String[] args) {&nbsp; &nbsp; // define the directory that contains the text files&nbsp; &nbsp; String dir = "U:\\workspace\\git\\ZZ--Temp\\TextFiles";&nbsp; &nbsp; Path dirPath = Paths.get(dir);&nbsp; &nbsp; // predefine some lines to be appended to every file&nbsp; &nbsp; List<String> linesToBeAppended = new ArrayList<>();&nbsp; &nbsp; linesToBeAppended.add("Hello new line in the file!");&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; // go through all files in the directory (tested with .txt files only)&nbsp; &nbsp; &nbsp; &nbsp; Files.list(dirPath)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // filter only files&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(Files::isRegularFile)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .forEach(filePath -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // append the predefined text to the file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Files.write(filePath, linesToBeAppended, StandardOpenOption.APPEND);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.err.println("Could not append text to file "&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + filePath.toAbsolutePath().toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; System.err.println("Could not list files in "&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + dirPath.toAbsolutePath().toString());&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}不幸的是,由于 Java 8 功能的不同范围,嵌套try-是必需的。它很丑陋,但优点是您可以通过列出文件或访问文件来区分抛出的 s 。catchforEachException编辑如果要在文件中添加新的第一行,则必须读取并重写文件。请参阅此示例,它与第一个示例略有不同:public static void main(String[] args) {&nbsp; &nbsp; // define the directory that contains the text files&nbsp; &nbsp; String dir = "U:\\workspace\\git\\ZZ--Temp\\TextFiles";&nbsp; &nbsp; Path dirPath = Paths.get(dir);&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; // go through all files in the directory (tested with .txt files only)&nbsp; &nbsp; &nbsp; &nbsp; Files.list(dirPath)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // filter only files&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(Files::isRegularFile)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .forEach(filePath -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // predefine some lines to be appended to every file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<String> linesToBeAppended = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add the first line as predefined first line&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; linesToBeAppended.add("Hello another line in the file!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // then read the file and add its lines to the list with&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // that already contains the new first line&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; linesToBeAppended.addAll(Files.readAllLines(filePath));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // append the extended text to the file (again),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // but this time overwrite the content&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Files.write(filePath, linesToBeAppended,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StandardOpenOption.TRUNCATE_EXISTING);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.err.println("Could not append text to file "&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + filePath.toAbsolutePath().toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; System.err.println("Could not list files in "&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + dirPath.toAbsolutePath().toString());&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}另一个重要的区别是 中的标志Files.write,它不再APPEND存在,但是TRUNCATE_EXISTING因为您将文件读入String表示行的列表中,然后将该集合添加到已经包含新的第一行的集合中。之后,您只需再次编写这些行,包括新的第一行。

慕仙森

我认为您想要做的是列出目录中的所有文件,然后处理每个文件。如果是这样的话你可以做File[] children = dir.listFiles();for (File child: children) {&nbsp; &nbsp; if (child.isFile()) {&nbsp; &nbsp; &nbsp; &nbsp; // append text&nbsp; &nbsp; }}我已经省略了附加数据的代码,因为您说您已经知道该怎么做。在这一点上,它只是将代码应用于每个文件的情况

30秒到达战场

您可以创建一个包含目录的文件:File directory = new File("pathOfYourDirectory");然后你可以得到所有的从属文件和目录:File[] subDirectories = directory.listFiles();现在你可以遍历这个数组了。但是你应该检查每个文件是否是一个文件。然后您可以获取此文件并附加文本。for (File subDir: subDirectories ) {&nbsp; &nbsp; if (subDir.isFile()) {&nbsp; &nbsp; &nbsp; &nbsp; // do your stuff&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答