逐行读txt文件(读写文件try catch finally 处理空行,编码格式,文件流释放问题,处理读到重复问题)
2015-09-15 10:33
716 查看
InputStreamReader read = null; BufferedReader bufferedReader = null; try { String encoding="utf-8"; read = new InputStreamReader(new FileInputStream(serverFile),encoding);//考虑到编码格式,<span style="font-family: Arial, Helvetica, sans-serif;">serverFile 将要读取的文件的目录</span> bufferedReader = new BufferedReader(read); String lineTxt = null; while((lineTxt = bufferedReader.readLine()) != null){ if (tempSet.add(lineTxt)) { //tempSet 就是 Set<String> tempSet = new HashSet<String>(); if (lineTxt.isEmpty()) {//处理空行 continue; } allRulesList.add(lineTxt); } } } catch (Exception e) { return "读取文件内容出错:" + e.getMessage(); }finally{ try {
if (bufferedReader != null) { bufferedReader.close(); } } catch (Exception e) { return "关闭文件流出错:" + e.getMessage(); } }
逐行读取.txt文件里面的不重复数据到一个list里面,再加工,处理读到空行的问题,处理读到数据重复问题,处理文件流释放不合理问题。处理编码格式问题。
if (read != null) {
read.close();
}
在最后还得加上这个,关掉read。
link:给推荐几个对程序员来说神器般存在的软件
相关文章推荐
- STL容器list
- 忍忍
- Qt 学习之路:QFileSystemModel
- 我爱上了我的司机 (5)
- Spring-data-redis + jedis环境
- 【MySQL】MySQL复制表结构、表数据
- js简单运动框架--2 多属性运动
- 跨域问题解决部分解决办法
- 常用的.net开源项目
- UltraEdit中文乱码的解决方法
- iOS程序员如何面试
- AFNetwork 作用和用法详解
- 微信开发大坑之数据无法正常存储至数据库
- 实战使用Axure设计App,使用WebStorm开发(6) – 迈向后端
- [转] 如何应用设计模式设计你的足球引擎(一和二)----Design Football Game(Part I and II)
- Scala中隐式类代码实战详解之Scala学习笔记-53
- linux安装git
- POJ1753 Flip Game【高斯消元法】
- VR
- 让Activiti记录变量历史信息