读书人

java 依据路径读取txt文件的办法

发布时间: 2012-09-10 22:20:13 作者: rapoo

java 根据路径读取txt文件的办法
根据txt文件路径,读取内容的方法:
1.txt文件在本地磁盘中存放:

public static void test1() {String path = "d:\\testfile.txt";File file = new File(path);try {BufferedReader rd = new BufferedReader(new FileReader(file));String str = "";String s = rd.readLine();while (null != s) {str += s + "<br/>";s = rd.readLine();}System.out.println(str);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}} 

2.从数据库中获取txt文件的路径后,然后把内容显示到页面上的方法:这个方法是struts2的Action类中的方法。

/**从数据库中读取txt文件--------------start------------------------**/String str = "";for(T_multimedia multimedia:list_multimedia){if(multimedia.getMulttype()==4){@SuppressWarnings("deprecation")String path = ServletActionContext.getRequest().getRealPath(multimedia.getMultpath()); //获取文本的绝对路径File file = new File(path);try {BufferedReader rd = new BufferedReader(new FileReader(file));String s = rd.readLine();while (null != s) {str += s;s = rd.readLine();}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}/**从数据库中读取txt文件--------------end------------------------**/setAttribute("str", str);//把文本内容存放到容器中,                     //利用el表达式在页面上显示文本内容 
















读书人网 >编程

热点推荐