Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By JosAH

Thread: BufferedReader \n operator

  1. #1
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default BufferedReader \n operator

    I have to read a .fj(basicly a .txt file) file with line(s) and process them into ONE string.

    So this is my code to try and fix the "problem" with BufferedReader where it throws away the \n character.

    Java Code:
    private static void BuffReader() throws IOException
    	{
    		BufferedReader read = new BufferedReader(new FileReader("bother.fj"));
    		StringBuilder build = new StringBuilder();
    		String temp;		
    		while ((temp = read.readLine()) != null) {
    			if (read.readLine() != null) {   //Tries to read next line, if empty \n will not be added
    				build.append(temp + "\n");   
    			}else build.append(temp);			
            }
    		read.close();
    		Program = build.toString();
    	}
    But this code does not give me the desired results. Can anyone help me with this one.
    PS: This is the first time I'm working with StringBuilder and BufferedReader so please go easy on me. :]

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: BufferedReader \n operator

    What exactly do you mean when you say it doesn't give you the desired results? What are the desired results? What does it do instead?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: BufferedReader \n operator

    You have two readLine() calls, one for the while loop and one for the if statement. Those two calls read two consecutive lines. Did you intend to do that?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: BufferedReader \n operator

    What exactly do you mean when you say it doesn't give you the desired results? What are the desired results? What does it do instead?
    I want it to read a String and then use builder, which works. But I have to make it a multistring String and since bufferedReader does not use the \n char when it reads I have to add it manually.
    Example:

    Hello
    World, I will kill you
    yes you will die.

    and the output is:

    Helloyes you will die.
    Last edited by Asmicor; 08-20-2013 at 07:39 PM.

  5. #5
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: BufferedReader \n operator

    You have two readLine() calls, one for the while loop and one for the if statement. Those two calls read two consecutive lines. Did you intend to do that?

    db
    Yes, the first one is to actually read the line, and the second one is to try and see if there is a next line.
    Now that you mention it, it reminds me that I forgot about the mark() and reset().

    But even when I tried that I still not get what I wanted

  6. #6
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: BufferedReader \n operator

    Yes, the first one is to actually read the line, and the second one is to try and see if there is a next line.
    Now that you mention it, it reminds me that I forgot about the mark() and reset().

    But even when I tried that I still not get what I want
    you are reading two line and appending one thats why you are getting wrong output ..
    Java Code:
    if (read.readLine() != null)
    write it as :
    Java Code:
    if (temp != null)
    Last edited by allaudin; 08-21-2013 at 09:14 AM.

  7. #7
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: BufferedReader \n operator

    and what is this
    Java Code:
    BufferedReader read = new BufferedReader(new FileReader("bother.fj"));
    "fj " extention .. never heard about that
    Last edited by allaudin; 08-21-2013 at 09:13 AM.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: BufferedReader \n operator

    Extensions don't mean anything, you can make one up as you like it to be. You can rename an Excel file to a .drbfgf file and it is still an Excel file. Only on Windows do they really mean anything since the OS uses them to bind a default program to them. A smart operating system looks at the header content of the file to know what it is.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: BufferedReader \n operator

    If you don't mind OS dependent end of line sequences, why don't you read that file character by character or block by block until you've read the entire file? You can even repair \r\n sequences afterwards. All this hoopla is ony necessary if you want to anticipate for incomplete last lines (if no end of line sequence there).

    kind regards,

    Jos
    gimbal2 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: BufferedReader \n operator

    Quote Originally Posted by gimbal2 View Post
    Extensions don't mean anything, you can make one up as you like it to be. You can rename an Excel file to a .drbfgf file and it is still an Excel file. Only on Windows do they really mean anything since the OS uses them to bind a default program to them. A smart operating system looks at the header content of the file to know what it is.
    thanks ...

Similar Threads

  1. Replies: 5
    Last Post: 01-22-2012, 12:02 AM
  2. Using BufferedReader
    By hqt in forum New To Java
    Replies: 1
    Last Post: 12-09-2011, 05:12 AM
  3. use of BufferedReader?please help
    By Cosmos in forum New To Java
    Replies: 11
    Last Post: 07-17-2011, 05:55 PM
  4. BufferedReader
    By Nerijus in forum New To Java
    Replies: 4
    Last Post: 04-26-2010, 08:28 PM
  5. BufferedReader
    By vidhya.sk in forum New To Java
    Replies: 2
    Last Post: 09-18-2008, 02:57 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
  •