Two Laptop Bag*
The moose likes I/O and Streams and the fly likes Problem reading a file through BufferedReader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Problem reading a file through BufferedReader" Watch "Problem reading a file through BufferedReader" New topic
Author

Problem reading a file through BufferedReader

amitjavaranch amitjavaranch
Greenhorn

Joined: May 26, 2005
Posts: 1
Hi,

I have a problem reading a txt file. An example string from the file is: "amit\nkumar".
But If I read this in, with a BufferedReader this becomes

Actual Output :
"amit\nkumar"

Expected Output :
"amit
kumar"

Why is this so?

The code is the following:

BufferedReader reader= new BufferedReader(
new InputStreamReader(
new FileInputStream(filename));
while (reader.ready()) {
String line= reader.readLine();
}

Any hints what is going wrong? It must be something with the input stream, since writing the String directly into the code works.

Amit Kumar
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
When you make a string with "\n" in your source code, the compiler sees the backslash as an escape character to indicate something special will follow. It translates the \n into some combination of 0x000D and 0x000A depending on the OS.

When you put the characters backslash and n into a data file they are really those characters. If you want to turn them into newlines, you'll have to replace "\n" with a newline.

Take a deep breath; this gets confusing. The JDK 1.4 and later String.replace uses regular expressions. To put a backslash in an expression you have to escape it with another backslash: \\n. And to put a backslash in a Java string you have to escape it with another, so we get "\\\\n" to match the characters in your file. Try this:

lineIn = lineIn.replace("\\\\n", "\n");


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
nana Amr
Ranch Hand

Joined: May 18, 2005
Posts: 53
lineIn = lineIn.replace("\\\\n", "\n");
plz can you explain more ,by a code if you can
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
A slight mod to your code ...

See what that does.

First argument: Java sees \\ and replaces it with \, giving four characters "\\\n". It sees another \\ and replaces it with \ giving three characters "\\n". This is the regular expression passed to replace(). The regex interpreter sees \\ and replaces it with \ giving two characters "\n". It looks for these two characters in the input.

Second argument: Java sees "\n" and replaces it with the newline character.

So replace() replaces the two characters "\n" with the newline character.

If you got all that, take the afternoon off, go have a beer.
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
If you read a text file in a texteditor and you can see \n in that text, it does not mean that if you read that file using java that it will read a linefeed character. No, it will actually read 2 characters a "\" (backslash) and a "n". If you want the file to be separated using linefeeds you actually have to put a real linefeed in there (RETURN) between the lines.

If you want to use the "\n" as line separator you will have to do some post processing after you have read the data and replace all "backslash-n" Strings with linefeed characters ('\n').
 
 
subject: Problem reading a file through BufferedReader
 
Similar Threads
Problem reading a file
BufferedReader.readLine() skips lines
Remove unwanted double quote from .csv file
First character of a File Becomes "?" on reading
i cant ...... learn to use the split