如何用jsp文件读取1个文件夹里所有txt文件的内容?

请问如何使用jsp文件读取一个文件夹里所有txt文件的内容?

这是一个文件里所有txt文件

img

######我目前只做到了读取文件夹里1个txt文件的内容

<%--
  Created by IntelliJ IDEA.
  User: 保密
  Date: 2022/11/16
  Time: 10:28
  To change this template use File | Settings | File Templates.
--%>

<%@ page import="java.io.BufferedReader"%>
<%@ page import="java.io.FileReader"%>
<%@ page import="java.io.File"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<html>
  <head>
    <title>使用JSP读取TXT文件</title>
  </head>
  <body>
  <%
    String path ="E:\\track\\2022\\01\\10\\";   // 这边文件目录需改成相对路径
    File file = new File(path,"1_%B4%A8A02Q8X.txt");
    FileReader fr = new FileReader(file);  //字符输入流
    BufferedReader br = new BufferedReader(fr);  //使文件可按行读取并具有缓冲功能
    StringBuffer strB = new StringBuffer();   //strB用来存储jsp.txt文件里的内容
    String str = br.readLine();
    while(str!=null){
      strB.append(str).append("<br>");   //将读取的内容放入strB
      str = br.readLine();
    }
    br.close();    //关闭输入流
  %>
  <div style="text-align: center;">
    <%=strB %>
  </div>
  </body>
</html>


只能读取1个txt文件内容,不知道怎么读取1个文件夹里所有txt文件的内容
我尝试过使用for循环的方法,但是在各大网站上都没有找到解决方法
希望大家能够帮忙,解决如何用jsp文件读取一个文件夹里所有txt文件的内容,感激不尽!

File 类里边有个 list方法或者使用listFiles获取File的数组列表 你把你代码换成这个

    String path ="E:\\track\\2022\\01\\10";   // 这边文件目录需改成相对路径
    File file = new File(path);
   file.listFiles() 就获取到了当前文件夹内所有的文件的File对象 , 然后你就遍历读取文件把