因为项目需要 所以才找了个获取txt文本内容的方法 并根据项目需求 做了分行读取的修改 方法如下
/* * 读取txt文本的工具 * */ public String ReadTxtFile(String strFilePath) { String path = String.valueOf(strFilePath); String content = ""; //文件内容字符串 StringBuffer sb = new StringBuffer(); //打开文件 File file = new File(path); //如果path是传递过来的参数,可以做一个非目录的判断 if (file.isDirectory()) { } else { try { InputStream instream = new FileInputStream(file); if (instream != null) { String line=null; BufferedReader buffreader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"GB2312")); //分行读取 line = buffreader.readLine(); if(line != null) onetxt = line+"\n"; while ((line = buffreader.readLine()) != null) { content += line + "\n"; } instream.close(); } } catch (java.io.FileNotFoundException e) { Log.d("TestFile", "The File doesn't not exist."); } catch (IOException e) { Log.d("TestFile", e.getMessage()); } } return content; }
使用的时候 直接调用方法可以了
ReadTxtFile(""); //文本所在文件夹的路径