1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 从文件夹中读取所有文件的指定内容 按行读取。

从文件夹中读取所有文件的指定内容 按行读取。

时间:2020-09-22 02:12:36

相关推荐

从文件夹中读取所有文件的指定内容 按行读取。

import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;public class ReadFromFile {public static void main(String[] args) {//文件绝对路径String fileName="C:\\Users\\liujun\\Desktop\\platform_machine";readFileByLines(fileName);}/*** 以行为单位读取文件,常用于读面向行的格式化文件*/public static void readFileByLines(String fileName) {File file = new File(fileName);File[] tempList = file.listFiles();// System.out.println("该目录下对象个数:"+tempList.length);for (int i = 0; i < tempList.length; i++) {if (tempList[i].isFile()) {//System.out.println("文件:"+tempList[i]);BufferedReader reader = null;try {//System.out.println("以行为单位读取文件内容,一次读一整行:");//读取指定文件夹下的每一个文件reader = new BufferedReader(new FileReader(tempList[i]));String tempString = null;int line = 1;// 一次读入一行,直到读入null为文件结束while ((tempString = reader.readLine()) != null) {// 判断包含ERROR的打印出来if(tempString.indexOf("[ERROR] (JdbcAgent.java:398)---function update [ INSERT")== -1){}else {//截取一行的开始位置String a="'010002'";//截取一行的结束位置String b="]";//打印每个文件的第几列和截取的数据//System.out.println("line " + line + ": " + tempString.substring(tempString.indexOf(a),tempString.lastIndexOf(b) ));//打印每行截取的数据System.out.println(tempString.substring(tempString.indexOf(a),tempString.lastIndexOf(b)));}line++;}reader.close();} catch (IOException e) {e.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e1) {}}}}//判断是否还包含文件夹if (tempList[i].isDirectory()) {System.out.println("文件夹:"+tempList[i]);}}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。