web开发,读取txt文件(程序或本地硬盘)

来源:互联网 发布:windows xp 反复重启 编辑:程序博客网 时间:2024/05/16 05:15
<%//本地硬盘下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>");   //将读取的内容放入strBstr = br.readLine();lists.add(str);}br.close();    //关闭输入流%>

0 0
原创粉丝点击