java读取.txt文件工具类FileUtiles
2024-10-19 06:20:25
public class FileUtils { private static final String ENCODING = "UTF-8";//编码方式 /**
* 获取文件的行
*
* @param fileName
* 文件名称
* @return List<String>
*/
public static String getContentByLine(String fileName) {
StringBuffer lines = new StringBuffer();
InputStreamReader read = null;
BufferedReader bufferedReader = null;
try {
String configPath = FileUtils.class.getClassLoader().getResource(fileName).getPath();
configPath = configPath.replaceAll("%20", " ");// 处理文件路径中空格问题
File file = new File(configPath);
if (file.isFile() && file.exists()) { // 判断文件是否存在
read = new InputStreamReader(new FileInputStream(file), ENCODING);
bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
if (lineTxt == null || lineTxt.length() == 0) {
continue;
}
lines.append(lineTxt);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (read != null) {
read.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return lines.toString();
}
}
最新文章
- C#-WinForm-用户控件如何获取父级窗体
- DBCP连接池使用问题
- 【转】SQL 操作类
- 实现多项式的JAVA类
- onRetainNonConfigurationInstance和getLastNonConfigurationInstance
- ASP.NET中默认的一级目录
- 超级密码(bfs)
- Maven中有三大模块
- 优雅的使用Chrome调试Node程序
- 1、opencv-2.4.7.2的安装和vs2010的配置
- 1分钟解决VS每次运行都显示“正在还原nuget程序包”问题
- 极速搭建RTMP直播流服务器+webapp (vue) 简单实现直播效果
- [Python数据挖掘]第5章、挖掘建模(下)
- MySQL 并发测试中,线程数和数据库连接池的实验
- OGG文件获取创建日期
- 域名映射ip
- ThinkPHP问题收集:模板中使用U方法时无法嵌套大括号,For标签,插入数据,新增的表字段缓存问题
- javascript中创建对象的几种不同方法
- JavaWeb基础—JSP自定义标签入门
- jQuery的表单选择器
热门文章
- 【Linux-Redhat】新手需要知道的Linux命令
- Unique Morse Code Words
- 案例一(haproxy+keepalived高可用负载均衡系统)【转】
- 题解-AtCoder-agc003F Fraction of Fractal(非矩阵快速幂解法)
- 使用sudo而无需输入密码的设置
- 函数-->;指定函数--->;默认函数--->;动态函数-->; 动态参数实现字符串格式化-->;lambda表达式,简单函数的表示
- LabVIEW中下拉列表和枚举的区别(两点)
- signal &; slot
- 软件包.deb的安装及卸载------dpkg
- 用Python优雅的处理日志