Read file until EOF from offset each time
Aight. This is my question :
Code :
// 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 :
Code :
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?
Re: Read file until EOF from offset each time
If at all possible it seems easier to try and create an input/output connection between what's writing the file and your program - cut out the middle man so to speak - but again that may not be possible. RandomAccessFile (Java Platform SE 6) would allow you to offset the pointer easily using the seek() method, and you can use readByte() (or any number of its other functions) which returns the number of bytes read) in a while loop and to not only read but tally up the number of bytes as you go so you can call seek on that location later on. If you want to use the read function you mentioned recall it returns the number of bytes read, so you can use an array of a designated size in a loop to continually read and after the call know how much was read into the array each time.
Re: Read file until EOF from offset each time
Thanks for the informative post!
Quote:
If at all possible it seems easier to try and create an input/output connection between what's writing the file and your program - cut out the middle man so to speak - but again that may not be possible.
I'm really not sure what you're even trying to mean here..
Quote:
RandomAccessFile (Java Platform SE 6) would allow you to offset the pointer easily using the seek() method, and you can use readByte() (or any number of its other functions) which returns the number of bytes read) in a while loop and to not only read but tally up the number of bytes as you go so you can call seek on that location later on.
Thanks a ton. I'm reading up on RandomAccessFile and would let you know whatever I achieve of!
I'll keep yer guys updated! :cool:
Re: Read file until EOF from offset each time
Glad to help.
Quote:
Originally Posted by
Pr0ject-Rec0n
I'm really not sure what you're even trying to mean here..
What I mean is what is writing the file? Another program? A Client? Is it possible to directly communicate with whatever is writing the file through an Input/Output stream?
Re: Read file until EOF from offset each time
Quote:
Originally Posted by
copeg
Glad to help.
What I mean is what is writing the file? Another program? A Client? Is it possible to directly communicate with whatever is writing the file through an Input/Output stream?
Oh, that's from another program altogether. I believe we have no control of that, whatsoever.