Results 1 to 7 of 7
  1. #1
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Question Two '\n' characters between lines while reading File

    Well, here goes my piece of code to read characters from a text file.

    Java Code:
    		File addressFile = new File("D://premdas/Desktop/address.txt");
    		try {
    			FileReader addressFileReader = new FileReader(addressFile);
    
    			int c;
    			while ((c = addressFileReader.read()) != -1) {
    				System.out.println("reading--"+(char)c+"--");
    			}
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    And address.txt contains this

    Java Code:
    No.2, 3rd floor, 7th main,
    E Block, Whitefield,
    Bangalore, India
    PIN - 560094
    A portion of the output is as follows

    Java Code:
    reading--i--
    reading--n--
    reading--,--
    reading--
    --
    reading--
    --
    reading--E--
    reading-- --
    reading--B--
    reading--l--
    This portion contains the ending part of first line, newline character and beginning portion of second line. As you can see the newline character is printed twice. But I'm sure in the address.txt, there are no empty lines between each line and each line immediately follows the other.

    Can anybody tell why I am getting two newline characters while reading.

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

    Default

    Are you sure those are two \n characters? Print the Unicode of each character and see for yourself:

    Java Code:
    System.out.println("reading--"+c+"--");
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Thanks.
    I checked. The first one is 10 (carriage return) and second is 13 (newline).
    I have no idea about carriage return, what it is and why is it here.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I have no idea about carriage return, what it is and why is it here.

    Windows uses this sequence of characters to separate lines.

  5. #5
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Thanks, got it now.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by subith86 View Post
    Thanks.
    I checked. The first one is 10 (carriage return) and second is 13 (newline).
    I have no idea about carriage return, what it is and why is it here.
    As Pbrockway2 already wrote: it's MS Windows way to terminate a line; are you sure it's 10 followed by 13 and not the other way around? I thought the sequence is \r\n (13 10) ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

Similar Threads

  1. problem reading a files lines
    By j187 in forum New To Java
    Replies: 1
    Last Post: 12-17-2010, 12:43 AM
  2. Reading Characters from a ByteBuffer
    By Dan0100 in forum New To Java
    Replies: 2
    Last Post: 09-20-2010, 10:05 PM
  3. Reading specific lines
    By ivvgangadhar in forum New To Java
    Replies: 8
    Last Post: 01-12-2009, 08:53 AM
  4. writing and reading unicode characters from a file
    By ranoosh in forum Advanced Java
    Replies: 4
    Last Post: 09-28-2008, 04:34 AM
  5. Replies: 3
    Last Post: 12-17-2007, 02:35 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
  •