webnovel
在Java中读取文件夹下的多个txt文件,可以按照以下思路进行: 首先,要获取指定文件夹下的所有txt文件。可以使用`File`类的`listFiles`方法将文件夹下的子文件取出放到`File[]`数组里,然后遍历这个数组,筛选出txt文件。 接着,对于每个txt文件,可以使用`FileReader`和`BufferedReader`来读取文件内容。例如: ```java import java.io.BufferedReader; import java.io.File; import…

Read multiple txt files under the folder with java.com.

2026-01-09 20:12
2026-01-10 00:16

在Java中读取文件夹下的多个txt文件,可以按照以下思路进行: 首先,要获取指定文件夹下的所有txt文件。可以使用`File`类的`listFiles`方法将文件夹下的子文件取出放到`File[]`数组里,然后遍历这个数组,筛选出txt文件。 接着,对于每个txt文件,可以使用`FileReader`和`BufferedReader`来读取文件内容。例如: ```java import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class ReadMultipleTxtFiles { public static void main(String[] args) { File folder = new File("your_folder_path"); File[] listOfFiles = folder.listFiles(); if (listOfFiles!= null) { for (File file : listOfFiles) { if (file.isFile() && file.getName().endsWith(".txt")) { try { FileReader fileReader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(fileReader); String line; while ((line = bufferedReader.readLine())!= null) { // 在这里可以对读取到的每行内容进行处理,例如打印或者其他操作 System.out.println(line); } bufferedReader.close(); fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } } } } } ``` 另外,还有一种方式是在处理编码格式时,如采用`GBK`编码读取文件内容,示例代码如下: ```java import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Set; public class ReadTxtFiles { // 读取指定目录下的txt文件,并添加到set里面(即去掉重复元素) public static Set Reader(String path) { Set setStr = new HashSet<>(); try { String encoding = "GBK"; File file = new File(path); String[] fileList = file.list(); for (String fl : fileList) { String newPath = path + "\\" + fl; InputStreamReader read = new InputStreamReader(new FileInputStream(newPath), encoding); BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while ((lineTxt = bufferedReader.readLine())!= null) { if (!lineTxt.trim().equals("")) { setStr.add(lineTxt); } } read.close(); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } return setStr; } public static void main(String[] args) { System.out.println("set>>>" + Reader("your_folder_path")); } } ``` 上述代码展示了两种读取文件夹下多个txt文件的基本方式,可以根据具体需求进行调整和扩展。 点击前往免费阅读更多精彩小说

Latest Questions

When does the story give Wen Jiaojiao the clearest consequence-heavy version of "The Mushroom-Picking Party of Six"?

If you want the clearest chapter reference, go to Chapter 160. Early on, As the sky gradually darkened, the originally mild drizzle began to...; later, the chapter leans on Jiang Li looked up at the sky and turned to the people behind him and....

1 answer
2026-06-05 08:28

When does the novel stop hinting and make "Establishing Authority" materially important for Miss Su?

If you want the clearest chapter reference, go to Chapter 204. Early on, Su Yinwan didn't move, but Director Qian turned around, ran to the...; later, the chapter leans on Seeing this, Su Yinwan clenched her fists in frustration..

1 answer
2026-06-05 08:28

Which chapter best shows how the power growth arc changes through "Jiang·Deceitful·Li is Verified"?

The best chapter pointer here is Chapter 198, "Jiang·Deceitful·Li is Verified". It opens with He wrapped his arms around himself and stepped back half a step, eyeing... and closes on How is that possible?" "Because I've calculated it, there are no Task....

1 answer
2026-06-05 08:28

At what point does "He Tampered with the Food" become a real turning point for Su Yinwan?

Look to Chapter 175 for the clearest answer. It begins with Wen Jiaojiao's eyes nearly rolled back into her head., keeps building through If you know you're sorry, why did you take so long and even redo your hair..., and lands on Su Yinwan, who had heard half his words, was a bit puzzled..

1 answer
2026-06-05 08:28

Which chapter should I read if I want the chapter where Jiang Li faces the key fallout of "Dear Brother"?

The best chapter pointer here is Chapter 197, "Dear Brother". It opens with Seeing that no one cared about what she said, Su Yinwan immediately felt... and closes on Wen Jiaojiao, aren't you underestimating me?.

1 answer
2026-06-05 08:28

When does the story give relationship tension the clearest consequence-heavy version of "Jiang Yan: There's a ghost"?

If you want the clearest chapter reference, go to Chapter 171. Early on, The afternoon rain stopped and started again, making the air even more...; later, the chapter leans on A few people simply moved the dining table into the small courtyard,.... By the close, They weren't any big shots;

1 answer
2026-06-05 08:28

When does the story give Su Yinwan the clearest consequence-heavy version of "Jiang·Deceitful·Li is Verified"?

If you want the clearest chapter reference, go to Chapter 198. Early on, He wrapped his arms around himself and stepped back half a step, eyeing...; later, the chapter leans on Is that not okay?" Jiang Yan: "....." "Just be normal, please..

1 answer
2026-06-05 08:28

At what point does "This is What Happens to Show-offs" become a real turning point for Shu Yan?

Look to Chapter 169 for the clearest answer. It begins with Everyone: "What?!" Seeing the shocked and bewildered expressions on their..., keeps building through This is a transformation show after all;

1 answer
2026-06-05 08:27

When does the novel stop hinting and make "The Goddess Who Can't Stand the Smell of Pig Manure" materially important for Shang Shaojing?

If you want the clearest chapter reference, go to Chapter 186. Early on, Seeing Wen Jiaojiao bravely entering the pigsty, Jiang Yan couldn't stay...; later, the chapter leans on He accepted being left behind by Jiang Li..

1 answer
2026-06-05 08:27
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z