java读取.txt文件工具类FileUtiles
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();
}
}
java读取.txt文件工具类FileUtiles的更多相关文章
- Java读取properties文件工具类并解决控制台中文乱码
1.建立properts文件(error.message.properties) HTTP201= 请求成功并且服务器创建了新的资源 2.在spring-mvc.xml文件(applicationCo ...
- Java读取txt文件
package com.loongtao.general.crawler.slave.utils; import java.io.BufferedReader; import java.io.File ...
- 读取Config文件工具类 PropertiesConfig.java
package com.util; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io ...
- java读取txt文件内容
package read; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public ...
- java 读取TXT文件的方法
java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...
- java读取TXT文件的方法
java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...
- java读取txt文件的2中方法---并将内容(每一行以固定的字符分割切成2段)存到map中去
#java读取txt文件的第一种方法 /** * 方法:readTxt * 功能:读取txt文件并把txt文件的内容---每一行作为一个字符串加入到List中去 * 参数:txt文件的地址 * 返回: ...
- Java读取txt文件信息并操作。
一.java读取txt文件内容 import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.Fi ...
- Java读取txt文件、excel文件的方法
Java读取txt文件.excel文件的方法 1.读取txt文件 public static String getFileContent(String filePath,String charset) ...
随机推荐
- Protobuf学习
https://www.jianshu.com/p/2265f56805fa https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.ht ...
- 20165237 2017-2018-2 《Java程序设计》第3周学习总结
20165237 2017-2018-2 <Java程序设计>第3周学习总结 教材学习内容总结 1.面向机器语言:类如汇编语言. 2.面向过程语言:类如C语言(但是相当于说话缺少主语). ...
- win10家庭版多用户
1.Windows 找不到gpedit.msc https://jingyan.baidu.com/article/54b6b9c08b08382d593b4747.html 2.win10家庭版 创 ...
- sqlmap 使用笔记
1.sqlmap -hh 查看详细说明 2.使用google proxychains sqlmap -g " inurl:\".php?id=1\" " 自动发 ...
- [转] ElasticSearch 常用的查询过滤语句
备忘remark https://www.cnblogs.com/ghj1976/p/5293250.html query 和 filter 的区别请看: http://www.cnblogs.co ...
- [转] Understanding Convolutional Neural Networks for NLP
http://www.wildml.com/2015/11/understanding-convolutional-neural-networks-for-nlp/ 讲CNN以及其在NLP的应用,非常 ...
- ProtonMiner挖矿蠕虫
特征 ProtonMail邮箱地址 利用漏洞 服务 漏洞 Hadoop 未授权访问 Drupal CVE-2018-7600 Redis 未授权访问 Spring Data Commons CVE-2 ...
- ES6学习笔记五(promise异步)
知识点1:rosolve是执行下一步then() // Promise { let ajax=function(){ console.log('执行2'); return new Promise(fu ...
- 使用命令行解析php文件
使用命令行解析php文件,这样可以调用Log4PHP库中的一些demo,因为默认的输出使用命令行作为输出. 建一个bat文件: echo 以下是使用命令行解析php文件 C:\xampp\php\ph ...
- telnetlib 中各种 read 函数的意义
基本原理 要明白 telnetlib 中各个 read 函数的意义,首先要了解 telnetlib 的工作原理. telnetlib 首先通过 socket 连接从网络接收数据,把数据存储到自己的 r ...