Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Read file until EOF from offset each time

  1. #1
    Junior Member Pr0ject-Rec0n's Avatar
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Read file until EOF from offset each time

    Aight. This is my question :

     			    // 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?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default 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.

  3. The Following User Says Thank You to copeg For This Useful Post:

    Pr0ject-Rec0n (April 2nd, 2010)

  4. #3
    Junior Member Pr0ject-Rec0n's Avatar
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Read file until EOF from offset each time

    Thanks for the informative post!

    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..

    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!

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Read file until EOF from offset each time

    Glad to help.

    Quote Originally Posted by Pr0ject-Rec0n View Post
    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?

  6. #5
    Junior Member Pr0ject-Rec0n's Avatar
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Read file until EOF from offset each time

    Quote Originally Posted by copeg View Post
    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.

Similar Threads

  1. end of file \ file offset
    By JayRay in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: March 27th, 2010, 04:11 PM
  2. Storing data from a socket to a file in real time
    By colossusdub in forum Java Networking
    Replies: 0
    Last Post: March 2nd, 2010, 08:10 AM
  3. Java program to read last line of a file
    By JavaPF in forum File Input/Output Tutorials
    Replies: 2
    Last Post: September 10th, 2009, 02:26 AM
  4. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 03:43 AM
  5. How to Read a Portion of a File in Java?
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 7th, 2009, 04:16 PM

Tags for this Thread