简体   繁体   中英

How do I use Java to incrementally read line from file?

I want to use Java to read line by line from input file. The logic of the code should be:

  1. The loadFile() in the log class reads the first line. The deltaRecords stores the first line
  2. In the Main class , I call loadFile() , and it only upload the data in the deltaRecords , which is the 1st line. The loadFile() waits until the first line has been analyzed by testRecords() in Main class
  3. The loadFile() in the log class reads the 2nd line. The deltaRecords stores the 2nd line
  4. In the Main class , I call loadFile() , and it only upload the data in the deltaRecords , which is the 2nd line. The loadFile() waits until the 2nd line has been analyzed by testRecords() in Main class
  5. The loadFile() in the log class reads the 3rd line. The deltaRecords stores the 3rd line.

For example, my input file is:

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

After I read TOM 1 2. I need to halt the process and do the analysis. Then I continue to read Jason 2 3.

Here is my current code. In the log class , I defined loadFile() to read the input line by line. In the Main class , I defined testRecords() to analyze.:

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();
       }

In my main function, I call loadfile:

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

Could anyone tell me how to solve this problem? Thank you very much.

You can solve it using a Consumer lambda function as input on your loadFile as follow:

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();
       }

Then when you call the loadFile function you can specify the analysis line logic as follow:

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);
});

You don't need to read a line when you create your line variable

Replace

String line = br.readLine();

with

String line = null;

You can use value assignment inside a conditional

Replace

while (line != null) {

with

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

In the case of empty lines continue

Replace

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

with

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

How your code should look like

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

You can use scanner to read the file and process line by line. I have attached snippet code below.

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);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM