Results 1 to 7 of 7
- 02-26-2011, 07:28 AM #1
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
Two '\n' characters between lines while reading File
Well, here goes my piece of code to read characters from a text file.
And address.txt contains thisJava 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(); }
A portion of the output is as followsJava Code:No.2, 3rd floor, 7th main, E Block, Whitefield, Bangalore, India PIN - 560094
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.Java Code:reading--i-- reading--n-- reading--,-- reading-- -- reading-- -- reading--E-- reading-- -- reading--B-- reading--l--
Can anybody tell why I am getting two newline characters while reading.
- 02-26-2011, 07:37 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,420
- Blog Entries
- 7
- Rep Power
- 17
Are you sure those are two \n characters? Print the Unicode of each character and see for yourself:
kind regards,Java Code:System.out.println("reading--"+c+"--");
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-26-2011, 07:43 AM #3
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
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.
- 02-26-2011, 07:48 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I have no idea about carriage return, what it is and why is it here.
Windows uses this sequence of characters to separate lines.
- 02-26-2011, 07:52 AM #5
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
Thanks, got it now.
- 02-26-2011, 08:11 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,420
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-26-2011, 09:56 AM #7
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
Similar Threads
-
problem reading a files lines
By j187 in forum New To JavaReplies: 1Last Post: 12-17-2010, 12:43 AM -
Reading Characters from a ByteBuffer
By Dan0100 in forum New To JavaReplies: 2Last Post: 09-20-2010, 10:05 PM -
Reading specific lines
By ivvgangadhar in forum New To JavaReplies: 8Last Post: 01-12-2009, 08:53 AM -
writing and reading unicode characters from a file
By ranoosh in forum Advanced JavaReplies: 4Last Post: 09-28-2008, 04:34 AM -
How to split a string into multiple lines of x characters each
By JackJ in forum New To JavaReplies: 3Last Post: 12-17-2007, 02:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks