<%
//本地硬盘下txt文件
//String path ="C:\\"; // 这边文件目录需改成相对路径
//File file = new File(path,"jsp.txt");
//程序WebContent下目录下txt文件
String filename2="/txt/"; filename2=this.getServletContext().getRealPath(filename2);
System.out.println(filename2);
//String path ="../txt/"; // 这边文件目录需改成相对路径
File file = new File(filename2,"jsp.txt");
FileReader fr = new FileReader(file); //字符输入流
BufferedReader br = new BufferedReader(fr); //使文件可按行读取并具有缓冲功能
//StringBuffer strB = new StringBuffer(); //strB用来存储jsp.txt文件里的内容
String str = br.readLine();
int num = 0;
List<String> lists=new ArrayList<String>();
lists.add(str);
while(str!=null){
//strB.append(str).append("<br>"); //将读取的内容放入strB
str = br.readLine();
lists.add(str);
}
br.close(); //关闭输入流
%>
web开发,读取txt文件(程序或本地硬盘)
最新推荐文章于 2023-08-18 01:40:42 发布