0$房天下问答|Java读写txt文件(来自长春标点小区)$https://m.fang.com/ask/ask_857085.html$https://static.soufunimg.com/common_m/m_public/201511/images/asksharedefault.png
packc/pages/ask/detail/detail?askid=857085
共1个回答
-
-
-
江山好的很
丨Lv 0
给你我写的两个方法!!作为参考吧! /** * @author CaoShun * @see 从指定位置文件中一行一行读取内容并把每行存入List集合 * */ public static List readInputByRow(String path) { List list=new ArrayList(); File file=new File(path); try { FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); BufferedReader reader = new BufferedReader(isr); String tempstring=""; while((tempstring=reader.readLine())!=null) { list.add(tempstring); //System.out.println("debug in ReadWriteOEMInfo.java on line 99 tempstring : "+tempstring); } reader.close(); isr.close(); fis.close(); return list; } catch (IOException e) { e.printStackTrace(); return null; } } /** * @author CaoShun * @see 从指定位置文件中,读指定一行数据 * */ public static String readInputByRow(String path,int num) { File file=new File(path); try { FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); BufferedReader reader = new BufferedReader(isr); String tempstring=""; int line=1; while((tempstring=reader.readLine())!=null) { if(line==num){ break; } line++; } reader.close(); isr.close(); fis.close(); return tempstring; } catch (IOException e) { e.printStackTrace(); return null; } }
免责声明:问答内容均来源于互联网用户,房天下对其内容不负责任,如有版权或其他问题可以联系房天下进行删除。

关注成功