remove NEWLINE at the end of the file

Discussion in 'Java' started by gk, Apr 4, 2006.

  1. gk

    gk Guest

    i use the code below to read a text file.

    try {
    BufferedReader in = new BufferedReader(new
    FileReader("infilename"));
    String str;
    while ((str = in.readLine()) != null) {
    process(str);
    }
    in.close();
    } catch (IOException e) {
    }


    but when the file has a NEWLINE at the END ....the code also read that.

    i just dont want to read that NEWLINE as soon as i come to the end.

    what i have to do ?
    gk, Apr 4, 2006
    #1
    1. Advertising

  2. gk

    Roedy Green Guest

    On 3 Apr 2006 19:05:04 -0700, "gk" <> wrote, quoted or
    indirectly quoted someone who said :

    >but when the file has a NEWLINE at the END ....the code also read that.
    >
    >i just dont want to read that NEWLINE as soon as i come to the end.


    you mean you are getting a "" on the tail end of the file?

    You may have multiple nls on the end of your file. Check with a hex
    editor and chop them.

    You may also just ignore 0-length lines.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Apr 4, 2006
    #2
    1. Advertising

  3. gk

    gk Guest


    >You may have multiple nls on the end of your file.....Check with a hex editor and chop them.



    cant i do it by programatically ?

    can i chop before reading the file by programatically ? How ?
    gk, Apr 4, 2006
    #3
  4. gk

    Roedy Green Guest

    On 3 Apr 2006 21:28:21 -0700, "gk" <> wrote, quoted or
    indirectly quoted someone who said :

    >can i chop before reading the file by programatically ? How ?


    You can write a tidier that chops blank lines off the tail end of the
    file. Whenever you read a blank line, increment a counter. Whenever
    you hit an non blank line, emit counter blank lines , zero the counter
    then emit the line.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Apr 4, 2006
    #4
  5. gk wrote:
    > i use the code below to read a text file.
    >
    > try {
    > BufferedReader in = new BufferedReader(new
    > FileReader("infilename"));
    > String str;
    > while ((str = in.readLine()) != null) {
    > process(str);
    > }
    > in.close();
    > } catch (IOException e) {
    > }
    >
    >
    > but when the file has a NEWLINE at the END ....the code also read that.
    >
    > i just dont want to read that NEWLINE as soon as i come to the end.
    >
    > what i have to do ?
    >


    If you know that you will never have a line in your data that is empty
    then just ignore it;

    while ((str = in.readLine()) != null) {
    if (!str.equals(""))
    process(str);

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Apr 4, 2006
    #5
  6. gk

    gk Guest

    I think this will look like much more impressive

    while ((str = in.readLine()) != null &&(!str.equals("")) {
    //if (!str.equals(""))
    process(str);

    Thnak you
    gk, Apr 5, 2006
    #6
  7. gk

    Roedy Green Guest

    On 5 Apr 2006 09:20:18 +0100, Gordon Beaton <> wrote,
    quoted or indirectly quoted someone who said :

    >process(str) || true


    what a strange way to code.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Apr 5, 2006
    #7
  8. On 4 Apr 2006 18:09:59 -0700, gk wrote:
    > I think this will look like much more impressive
    >
    > while ((str = in.readLine()) != null &&(!str.equals("")) {
    > //if (!str.equals(""))
    > process(str);


    If your goal is to impress, why stop there when you can put
    *everything* in the loop header?

    while ((str = in.readLine()) != null && (!str.equals("") && (process(str) || true)));

    /gordon

    (See also http://www.m-w.com/cgi-bin/dictionary?va=irony)

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Apr 5, 2006
    #8
  9. On Wed, 05 Apr 2006 08:07:56 GMT, Roedy Green wrote:
    > what a strange way to code.


    Follow the link I posted.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Apr 5, 2006
    #9
  10. gk

    gk Guest

    I apologige , if the adjective has hurt you. I understand, that was the
    improper usage after all. english is not my native language.

    I wanted to mean, how the code looks like if i put that way. Of course,
    the code has been borrowed from you but customized.

    language should not taken into account.

    thank you
    gk, Apr 11, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. av
    Replies:
    1
    Views:
    523
    Jack Klein
    Nov 20, 2003
  2. Replies:
    2
    Views:
    642
    Robert W Hand
    Jun 13, 2005
  3. Replies:
    8
    Views:
    521
    red floyd
    Jan 12, 2006
  4. Jimmy
    Replies:
    3
    Views:
    327
    J. J. Farrell
    Sep 9, 2011
  5. Jimmy
    Replies:
    13
    Views:
    649
    Peter Nilsson
    Sep 21, 2011
Loading...

Share This Page