Aight. I have a query regarding File I/O :

// Keep reading the file in a loop for some 100 seconds..
for (int x=0; x<100; x++) {
// Code for reading file - say readFile()
Thread.currentThread().sleep(1000); /* sleep for one second */
}

Now, I need to read a file (whose contents may keep changing over the course of the 100 seconds). Everytime I read the contents of the file, I need to get the new data (that's been added).
PS : No data will be deleted from the file. Data would only be added.

So, I assume I require to mark an offset and read from that offset until I encounter EOF. Every single time. How do I do this?

I'm using BufferedReader and there's this method :

public int read(char[] cbuf,
                int off,
                int len)

But, I do not know the len (length of characters) to be read each time. Remember, I need to read till EOF everytime. Someone suggested me to use the ready() method of BufferedReader, but I'm lost and do not know how to use it. Can someone put up a prototype example depicting this situation?

One other suggestion was not to use ready() but read until I encounter an Exception.. How do I use it if I want to go this way?

Recommended Answers

Place the read routine in a separate method (if not a separate thread, as well) and don't close the reader just because EOF is encountered. Once you need to read more from the file, simply call the read method again. If more data exists in the file, at that point, …

Jump to Post

All 3 Replies

Place the read routine in a separate method (if not a separate thread, as well) and don't close the reader just because EOF is encountered. Once you need to read more from the file, simply call the read method again. If more data exists in the file, at that point, it should start reading again, from the point where it left off last time.

Try this:

Open notepad, add a few lines text, and save that as "C:/bogus.txt" but don't close it.

Then start this program running

package readtest;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JOptionPane;

public class Main {

    private static void read(BufferedReader br) throws IOException {
        String s = null;
        while ((s = br.readLine()) != null) {
            System.out.println(s);
        }
    }

    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new FileReader("C:/bogus.txt"));
            try {
                int yes = JOptionPane.OK_OPTION;
                while (yes == JOptionPane.OK_OPTION) {
                    read(br);
                    yes = JOptionPane.showConfirmDialog(null, "Continue Reading?");
                }
            } finally {
                try { br.close(); } catch (Exception e) {}
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

when the confirm prompt comes, enter a few more lines txt and save (but don't close although you can, it is simply easier to keep it open), then press yes/ok on the confirm.

Repeat as often as you like. When finished press no/cancel on the confirm.

That was a brilliant idea. Simple yet effective.

I don't know why I didn't think of something on these lines before. I'm gonna try and improvise further on this one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.