How to read TXT to a certain point and then continue from that point?

1

I have a class that is responsible for reading a TXT file part by piece and returning a result according to that part.

This result will be used by another class. That is, I open the txt read until a certain part and make a return to another class. But when I call this class it reads again. I want to continue from where I left off in txt and not have to read it all over again.

Example:

I have the following text file:

  

"Hi, how are you? I would like to know your name
   My name is Joao "

I want my reading class to read this part:

  

"Hi, how's it going?"

Then when you call her again she will start reading from here:

  

"I'd like to know your name
   My name is Joao "

NOTE: This was just a hypothetical example.

Please help me. my code for reading is this:

 FileReader in = null;
    try {
        in = new FileReader(dir);
        BufferedReader br = new BufferedReader(in);
        String line;
        int lineNum = 1;
        while ((line = br.readLine()) != null) {
            verifyLine(line,lineNum);
            lineNum++;
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

The verify function takes the line and le only the parts I want. But when I give the return I come back from the beginning. In other words, if I keep within this class I can read the way I want. But if I return to another class, I already have problems.

    
asked by anonymous 20.11.2017 / 12:38

1 answer

1

You could save the BufferedReader reference for later use. I gave you an example so you have an idea:

package stackoverflow.tests;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class TxtReader implements Closeable {

    private BufferedReader reader;
    private boolean stop;
    private int count;
    private boolean endOfFile;

    public TxtReader(String path) throws FileNotFoundException {
        this.reader = new BufferedReader(new FileReader(path));
    }

    public void processar() throws IOException {
        stop = false;
        count++;
        String line;
        int lineNum = 1;
        System.out.println("\nLeitura "+count);
        while ((line = reader.readLine()) != null) {
            verifyLine(line,lineNum);
            if (stop) {
                return;
            }
            lineNum++;
        }
        endOfFile = true;
    }

    public boolean isEndOfFile() {
        return endOfFile;
    }

    private void verifyLine(String line, int lineNum) {
        System.out.println(line);
        if (line.equals("break")) {
            stop = true;;
        }
    }

    @Override
    public void close() throws IOException {
        reader.close();
    }

    public static void main(String args[]) throws IOException {
        TxtReader leitura = new TxtReader("teste.txt");
        while(!leitura.isEndOfFile()) {
            leitura.processar();
        }
        leitura.close();
    }

}

As I did not know what your reading logic was, I invented one where the stop condition is the break text. Every time the word break is found the read method will return. I put a condition to know if the file has reached the end. If you still have data to read, the reading will continue from where you left off. Test text:

    
21.11.2017 / 03:46