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 6 of 6

Thread: end of file \ file offset

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default end of file \ file offset

    hi,
    i am using BufferReader to read lines from a text file.
    i would like to create a boolean method names hasMore() which will be indicating if the end-of-file reached.
    here is a pesudo
    if(hasMore())
    line = br.readLine()

    and speaking of files, how do i get the offset within the open file which i've been reading its lines?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: end of file \ file offset

    Not sure why you wish to create the new method, all you really need to do is check if the readLine() method returns null.
    String line;
    while ( ( line = br.readLine() ) != null ){
    //line contains the line contents of the line just read
    }
    As for offset, don't think there is a way to do this directly through the reader object but you can tally what has been read so far and use this as the offset.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: end of file \ file offset

    If your buffer supports marking (I believe file buffers do), you can "dummy read" one character and then go back to your marked spot.

    public boolean hasMore(BufferedReader reader) throws IllegalArgumentException
    {
         if (reader.markSupported())
         {
              reader.mark(1);
              int val = reader.read();
              reader.reset();
              if (val == -1)
              {
                   return false;
              }
              else
              {
                   return true;
              }
         }
         else
         {
              // marking not supported
              throws new IllegalArgumentException();
         }
    }

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: end of file \ file offset

    first of all, thanks.
    i will try it now.

    now i have this to handle...
    i am opening a file using BufferedReader and march through the file once. after it, i have to return to the beginning of the file, and i would like to acheive it without closeing the file and reopen it.
    how can it be done?

    i am still wondering how come java became more complicated then c language...

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: end of file \ file offset

    Quote Originally Posted by JayRay View Post
    now i have this to handle...
    i am opening a file using BufferedReader and march through the file once. after it, i have to return to the beginning of the file, and i would like to acheive it without closeing the file and reopen it.
    how can it be done?
    Use the method Helloworld suggested...mark the bufferedreader when you open the file, read it, then call reset on the reader...have a look at these methods in the api of BufferedReader to learn specifically how to use them. You must pass a value to mark that indicates how far it expects to read while still preserving the mark
    i am still wondering how come java became more complicated then c language...
    I'm sure there are just as many others out there asking the opposite question

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: end of file \ file offset

    i am still wondering how come java became more complicated then c language...
    It's not, in Java you don't have to manage your memory manually, or worry about "cryptic" symbols, or linking, or multiple inheritance issues, or operator overloading, or pointers, or tons of OS-specific problems/compiler specific problems... etc. etc.

    Also, if you're reading through a whole file, I would strongly suggest against marking, especially for larger files. Marking basically creates a separate buffer that has the set amount of data you specify put in. This leads to two issues (well, there might be more):
    1. You have to know how big of a buffer you need. If you have access to the original file reference, this won't be too difficult of a task.
    2. Because you are creating a separate buffer, you're wasting a ton of memory just to store the file in memory twice.

    In java (and actually, in C/C++, too), it's not an uncommon practice to read through a whole file, closing it, and the re-opening that file for read again.

    If you want to access any part of a file at any time, I'd suggest looking at the Random Access File Class.
    Last edited by helloworld922; March 27th, 2010 at 04:14 PM.

Similar Threads

  1. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  2. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 09:11 AM
  3. Jar File Security
    By Symbols in forum Java Theory & Questions
    Replies: 1
    Last Post: February 28th, 2010, 09:48 PM
  4. Replies: 8
    Last Post: January 6th, 2010, 08:59 AM
  5. Java program to reduce spaces between the words in a text file
    By tyolu in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 13th, 2009, 07:17 AM