首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

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

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表达式在页面上显示文本内容 
















热点排行