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

Asked

Viewed 620 times

1

I have a class that is responsible for reading a TXT file part by part and return me a result according to this part.

This result will be used by another class. That is, I open txt I read to a certain part and I re-turn to another class. However when I call this class it reads again. I want to pick up where I left off in txt and n 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 are you? Like"

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

"would know his 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 reads only the parts I want. But when I give the Return I come back from the beginning. In other words, if I stay in this class I can read it the way I want to. But if I give Return to another class I already have problems.

  • Have you ever thought of Playing this text to a variable and deleting from it the part that has already been read ? Another option that came to mind was you have a parameter to inform from which position you should start reading, so just use the Content size that was returned +1 in this parameter, this option would serve ?

  • Is the control done by line or by character? In your example you are cutting in the middle of the word, but the code seems to be reading line by line.

  • yes I can do the control via character and I think your Christiano solution can help me

1 answer

1

You could save the Bufferedreader reference for later use. I made an example for you to 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();
    }

}

Since I didn’t know your reading logic, I invented one where the stopping condition is the break text. Every time the word break is found the reading 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 where it left off. Test text:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.