File file = new File(fileurl);
// 创建新数据
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");//NOSONAR
BufferedReader br = new BufferedReader(isr);//NOSONAR--禁止sonar扫描
String str;
int num=0;//记录日志可能会在那些地方遇到问题
while ((str = br.readLine()) != null) {// 按行读取
中间写自己的处理逻辑,加上trycatch捕捉异常
try{
String ss[] = str.trim().split("\\|");// 注意此处是“|”java中的特殊字符,要转换成正则。否则会出错。
}catch(Exceptino e) {
num++;
logger.info(“read data is errror,num=”num+"reason:"+e.getMessage());
}
num++;
}
br.close();// 关闭流
本文介绍如何使用Java进行文件读取操作,并通过UTF-8编码方式解析文本内容。文章详细展示了使用File、FileInputStream及BufferedReader等类实现逐行读取文件的方法,并加入了异常处理机制确保程序稳定性。
3321

被折叠的 条评论
为什么被折叠?



