Java - 读取文件夹中的所有.txt文件 [英] Java - Read all .txt files in folder
本文介绍了Java - 读取文件夹中的所有.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们说,我有一个名为 maps
的文件夹,在地图内
我有 map1 .txt
, map2.txt,
和 map3.txt
。如何使用Java和 BufferReader
读取文件夹中的所有
(如果可能的话)? .txt
文件地图
Let's say, I have a folder called maps
and inside maps
I have map1.txt
, map2.txt,
and map3.txt
. How can I use Java and the BufferReader
to read all of the .txt
files in folder maps
(if it is at all possible)?
推荐答案
以下内容应该让你去,请注意我使用apache commons FileUtils而不是为了简单而搞乱缓冲区和流...
Something like the following should get you going, note that I use apache commons FileUtils instead of messing with buffers and streams for simplicity...
File folder = new File("/path/to/files");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
File file = listOfFiles[i];
if (file.isFile() && file.getName().endsWith(".txt")) {
String content = FileUtils.readFileToString(file);
/* do somthing with content */
}
}
这篇关于Java - 读取文件夹中的所有.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文