我如何使用Java从文件中增量读取行?

问题描述 投票:0回答:3

我想使用Java从输入文件中逐行读取。代码的逻辑应为:

  1. 日志类中的loadFile()读取第一行。 deltaRecords存储第一行
  2. Main class中,我调用loadFile(),它仅将数据上传到第一行的deltaRecords中。 loadFile()等待,直到第一行由Mainclass中的testRecords()
  3. 分析
  4. 日志类中的loadFile()读取第二行。 deltaRecords存储第二行
  5. Main class中,我调用loadFile(),它仅将数据上传到第二行的deltaRecords中。 loadFile()等待,直到第二行由Mainclass中的testRecords()
  6. 分析
  7. 日志类中的loadFile()读取第三行。 deltaRecords存储第三行。

例如,我的输入文件是:

TOM 1 2 <br/>
Jason 2 3 <br/>
Tommy 1 4 <br/>

阅读TOM 1 2.之后,我需要停止该过程并进行分析。然后,我继续阅读Jason 23。

这是我当前的代码。在log class中,我定义了loadFile()逐行读取输入。在Main class中,我定义了testRecords()进行分析。:

public void loadFile(String filename) {
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(filename));
        Main main = new Main();
        String line = br.readLine();
        int count = 0;
        while (line != null) {
            if (line.length() == 0) {
                line = br.readLine();
                continue;
            }
            LogRecord record = new LogRecord(line);
            //add record to the deltaRecord
            deltaRecords.add(record);
            //here I need to wait until it has been analyzed. Then I continue read
           //initial the deletaRecords and add current line to the old record

            existRecords.add(record);
            line = br.readLine();
           }
       }catch(Exception e){
            System.out.println("load log file failed! ");
            e.printStackTrace();
       }

在我的主函数中,我调用loadfile:

Log log = new Log();
log.loadFile("data/output_01.txt");
QueryEngine.queryEngine.log = log;
testRecord(log);

有人可以告诉我如何解决此问题吗?非常感谢。

java io readfile
3个回答
0
投票

创建行变量时无需读取行

替换

String line = br.readLine();

with

String line = null;

您可以在条件内使用值分配

替换

while (line != null) {

with

while ((line = br.readLine()) != null) {

在空行的情况下继续

替换

            if (line.length() == 0) {
                line = br.readLine();
                continue;
            }

with

            if (line.length() == 0) {
                continue;
            }

您的代码应如何显示

String line = null;
while ((line = br.readLine()) != null) {
    if (line.length() == 0) continue;
    //Analyze your line
}

0
投票

您可以使用Consumer lambda函数作为loadFile上的输入来解决它,如下所示:

public void loadFile(String filename, Consumer<LogRecord> lineAnalyzer) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(filename));
            Main main = new Main();
            String line = br.readLine();
            int count = 0;
            while (line != null) {
                if (line.length() == 0) {
                    line = br.readLine();
                    continue;
                }
                LogRecord record = new LogRecord(line);
                //add record to the deltaRecord
                deltaRecords.add(record);
                //here I need to wait until it has been analyzed. Then I continue read
                //Analyze line
                lineAnalyzer.accept(record);
                existRecords.add(record);
                line = br.readLine();
           }
       }catch(Exception e){
            System.out.println("load log file failed! ");
            e.printStackTrace();
       }

然后,当您调用loadFile函数时,可以如下指定分析行逻辑:

Log log = new Log();
QueryEngine.queryEngine.log = log;
log.loadFile("data/output_01.txt",logRecord -> {
    System.out.println("Starting analysis on log record "+logRecord);
    // Analyze the log record
    testRecord(log);
});

-1
投票

您可以使用扫描仪逐行读取文件并进行处理。我在下面附加了代码段。

public void loadFile(String filename) {
        try {
            Scanner sc= new Scanner(new File(filename));
            while (sc.hasNext()) {
                String line = sc.nextLine();
                LogRecord record = new LogRecord(line);
                //add record to the deltaRecord
                deltaRecords.add(record);
                //here I need to wait until it has been analyzed. Then I continue read next line
               //Place your code here
            }
        } catch (FileNotFoundException fe) {
            System.err.println(fe);
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.