Results 1 to 4 of 4

Thread: end of file

  1. #1
    RayJay is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default end of file

    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.

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

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Way #1: count the characters you have read in (and don't forget to compensate for \r\n). problem: you'll run into trouble with character encodings

    way #2: we observe that BufferedReader returns null when end of file is reached. So what you can do is read one line ahead, like this:

    Java Code:
    class MyReader {
      private BufferedReader reader;
      private String currentLine;
      public MyReader (File file) {
        reader = new BufferedReader (...);
        currentLine = reader.readLine ();
      }
      public String nextLine () {
        if (currentLine == null) { 
          throw new Exception ("Reached the end of the file already!!!");
        }
        String toReturn = currentLine;
        currentLine = reader.readLine ();
        return toReturn;
      }
      public boolean hasMore () {
        return (currentLine != null); 
      }
    }

  3. #3
    RayJay is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    thanks for your help.
    you idea is right but it can't serve me right for my code.
    i thought to use the mark() and reset() methods, but the mark() method, as far as i know, should get for its argument the file offset.
    i tried to count the length() of the string i've read from the file, but as you mentiond, it does not contain any new line or other special symbols.

    could you help me figure it out? how can i read more line in advanced and still be in the same position?
    the issue is, that i have to preform the read() only after i know for sure i can read the line.

    how come java do not hold any method to peek() and the file, get the reading/writing offset of the file?

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    What part of my solution doesn't work for you? Consider it cause it would definitely be simplest.

    Now the thing is this: when you use BufferedReader, internally it holds a buffer that gets populated from the file. So, when you use BufferedReader.read(), you're not reading from the file but rather from that buffer. The FileReader is currently at some different point altogether.

    So you can't use BufferedReader at all.

    What you can use is FileInputStream. Note that InputStreams are for bytes, and Readers are for strings, but since you care about byte-wise position in the file, you need to use the Streams.

    So you can use FileInputStream, read bytes in one by one, look for \r\n, and construct the Strings accordingly... FileInputStream should support mark() and reset() methods, check the API.

Similar Threads

  1. Replies: 8
    Last Post: 02-18-2010, 01:54 PM
  2. retain value of input type file in a jsp file while being dynamically generated
    By nidhi c in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2009, 03:21 AM
  3. Replies: 2
    Last Post: 04-07-2009, 07:25 AM
  4. Replies: 0
    Last Post: 02-11-2009, 10:53 AM
  5. Replies: 7
    Last Post: 05-23-2008, 04:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •