Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default readLine(boolean)

    Hey!
    Does such a method readLine(boolean) exist in BufferedReader? I know it's not mentioned in the API, but when I write the following code:
    Java Code:
    BufferedReader br = new BufferedReader(new FileReader("C:/...../inp2.txt"));
    System.out.println(br.readLine(true));
    it says:
    The method readLine(boolean) from the type BufferedReader is not visible

    Which tells me that the method readLine(boolean) exists but I am not allowed to use it because it's private.

    I'm asking this because I want to use readLine specificallyto extract two lines separated by CRLF (inp2.txt contains the two lines separated by an enter) and I know there are other ways to do it, but just wondering if readLine always necessarily has to terminate a line with CRLF?

    Please help!

    Thanks :)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: readLine(boolean)

    What do you want the readLine(true) method to do that a simple readLine() call can't do?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: readLine(boolean)

    Hi Jos!

    Well, I'm trying to make the simple readLine() function read even if it encounters a '\n' in the input file. I understand there are other ways to do it, but I'm doing it as a challenge because I found that there's a function readLine(boolean) which is defined in the class BufferedReader but not visible outside it. In fact, the simple readLine() calls the readLine(boolean ignoreLF) with false as the boolean, which is why the simple readLine() does not ignore the '\n'. I want it to ignore the '\n'.

    Thanks!

    Regards,
    Shobit

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: readLine(boolean)

    What would the readLine method read if it ignores the newline character? The whole file into one String?

  5. #5
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: readLine(boolean)

    Yes, that's what I think it should do. Because the comment mentioned above the readLine(boolean ignoreLF) function says:

    /**
    * Reads a line of text. A line is considered to be terminated by any one
    * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
    * followed immediately by a linefeed.
    *
    * @param ignoreLF If true, the next '\n' will be skipped
    *
    * @return A String containing the contents of the line, not including
    * any line-termination characters, or null if the end of the
    * stream has been reached
    *
    * @see java.io.LineNumberReader#readLine()
    *
    * @exception IOException If an I/O error occurs
    */

    I'm also wondering why they had to make the simple readLine() call the readLine(boolean ignoreLF) with a "false" parameter. But not allow me to call the readLine(boolean) with a true parameter either directly or indirectly.

    For e.g., if I want to read a simple HTTP Request (including the request-line and the header together) using the readLine() and store the entire request in a String, the readLine(false) would have helped.
    Last edited by shobitbeltangdy; 10-02-2011 at 09:33 PM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: readLine(boolean)

    @param ignoreLF If true, the next '\n' will be skipped
    What do you do if you want to read the first 3 lines?
    IMHO Waste of time taking about "what if ...". Use what exists. Or write your own.

  7. #7
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: readLine(boolean)

    Thanks! That's true, I could write my own. But I was just wondering why did they make a function readLine(boolean) and call it from readLine() by passing a 'false' parameter.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: readLine(boolean)

    But I was just wondering why
    Ah yes, wondering. A question for the author. Have you read his autobiograhpy. Maybe he explains it there.

    I guess some of the code was written in a hurry. People had ideas that they never got a chance to come back to and implement.

Similar Threads

  1. readLine() not working.
    By space.puffin in forum Networking
    Replies: 15
    Last Post: 09-20-2011, 08:03 PM
  2. Replies: 3
    Last Post: 09-13-2011, 12:01 AM
  3. boolean error help when no boolean is given
    By drewtrcy in forum New To Java
    Replies: 18
    Last Post: 05-05-2011, 09:04 AM
  4. Replies: 10
    Last Post: 01-17-2009, 08:30 PM
  5. DataInputStream readLine()
    By ravian in forum New To Java
    Replies: 2
    Last Post: 11-26-2007, 10:44 PM

Posting Permissions

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