3 回答

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以java.nio結(jié)合使用 Java 8 功能來(lái)列出文件并為每個(gè)文件執(zhí)行一些操作。有一種方法可以將文本附加到文件。
請(qǐng)參閱此示例并閱讀代碼中的一些注釋:
public static void main(String[] args) {
// define the directory that contains the text files
String dir = "U:\\workspace\\git\\ZZ--Temp\\TextFiles";
Path dirPath = Paths.get(dir);
// predefine some lines to be appended to every file
List<String> linesToBeAppended = new ArrayList<>();
linesToBeAppended.add("Hello new line in the file!");
try {
// go through all files in the directory (tested with .txt files only)
Files.list(dirPath)
// filter only files
.filter(Files::isRegularFile)
.forEach(filePath -> {
try {
// append the predefined text to the file
Files.write(filePath, linesToBeAppended, StandardOpenOption.APPEND);
} catch (IOException e) {
System.err.println("Could not append text to file "
+ filePath.toAbsolutePath().toString());
e.printStackTrace();
}
});
} catch (IOException e) {
System.err.println("Could not list files in "
+ dirPath.toAbsolutePath().toString());
e.printStackTrace();
}
}
不幸的是,由于 Java 8 功能的不同范圍,嵌套try-是必需的。它很丑陋,但優(yōu)點(diǎn)是您可以通過列出文件或訪問文件來(lái)區(qū)分拋出的 s 。catchforEachException
編輯
如果要在文件中添加新的第一行,則必須讀取并重寫文件。請(qǐng)參閱此示例,它與第一個(gè)示例略有不同:
public static void main(String[] args) {
// define the directory that contains the text files
String dir = "U:\\workspace\\git\\ZZ--Temp\\TextFiles";
Path dirPath = Paths.get(dir);
try {
// go through all files in the directory (tested with .txt files only)
Files.list(dirPath)
// filter only files
.filter(Files::isRegularFile)
.forEach(filePath -> {
// predefine some lines to be appended to every file
List<String> linesToBeAppended = new ArrayList<>();
// add the first line as predefined first line
linesToBeAppended.add("Hello another line in the file!");
try {
// then read the file and add its lines to the list with
// that already contains the new first line
linesToBeAppended.addAll(Files.readAllLines(filePath));
// append the extended text to the file (again),
// but this time overwrite the content
Files.write(filePath, linesToBeAppended,
StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) {
System.err.println("Could not append text to file "
+ filePath.toAbsolutePath().toString());
e.printStackTrace();
}
});
} catch (IOException e) {
System.err.println("Could not list files in "
+ dirPath.toAbsolutePath().toString());
e.printStackTrace();
}
}
另一個(gè)重要的區(qū)別是 中的標(biāo)志Files.write,它不再APPEND存在,但是TRUNCATE_EXISTING因?yàn)槟鷮⑽募x入String表示行的列表中,然后將該集合添加到已經(jīng)包含新的第一行的集合中。之后,您只需再次編寫這些行,包括新的第一行。

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
我認(rèn)為您想要做的是列出目錄中的所有文件,然后處理每個(gè)文件。如果是這樣的話你可以做
File[] children = dir.listFiles();
for (File child: children) {
if (child.isFile()) {
// append text
}
}
我已經(jīng)省略了附加數(shù)據(jù)的代碼,因?yàn)槟f(shuō)您已經(jīng)知道該怎么做。在這一點(diǎn)上,它只是將代碼應(yīng)用于每個(gè)文件的情況

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以創(chuàng)建一個(gè)包含目錄的文件:
File directory = new File("pathOfYourDirectory");
然后你可以得到所有的從屬文件和目錄:
File[] subDirectories = directory.listFiles();
現(xiàn)在你可以遍歷這個(gè)數(shù)組了。但是你應(yīng)該檢查每個(gè)文件是否是一個(gè)文件。然后您可以獲取此文件并附加文本。
for (File subDir: subDirectories ) {
if (subDir.isFile()) {
// do your stuff
}
}
添加回答
舉報(bào)