PearlJamFanatic
asked on
Newline character in txt file(notepad) displayed as square character
public void appendToFile(String content){
try {
PrintStream outToFile = new PrintStream(new BufferedOutputStream(new FileOutputStream(fileHandle.getPath(), true)));
outToFile.println(content);
outToFile.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Unable to Write to File");
}
}
appendToFile("blah blah blah \r\n");
The file shows up fine in the eclipse editor but whyen opened in notepad the line breaks are displayed as square characters.
In which operating system are you running the code above?
ASKER
windows xp
>>appendToFile("blah blah blah \r\n");
Not sure where the above fits in ..?
Are you working on a system other that Windows and then opening the file in Notepad?
Notepad won't deal with non-Windows line feeds - use WordPad instead
Not sure where the above fits in ..?
Are you working on a system other that Windows and then opening the file in Notepad?
Notepad won't deal with non-Windows line feeds - use WordPad instead
Worthwhile attaching (not pasting) the text file in question if you can
ASKER
I am creating in xp and opening in xp. yes it opens up fine in wordpad.
Isn't there any way to do this such as it will be displyed properly in notepad.
Isn't there any way to do this such as it will be displyed properly in notepad.
ASKER
attached.
result.txt
result.txt
It seems that the file was generated in some unix environment and you are trying opening it on Windows.
If you do this, depending on how the file was transfered, the character "\n" is showed as a square in notepad.
If you do this, depending on how the file was transfered, the character "\n" is showed as a square in notepad.
I suspect 'content' has lone newlines ( '\n' ) embedded in it
ASKER
there was one lone \n instance which has been corrected now. The problem still persists however.
Yes, as i suspected, there are hardcoded newlines in the content. Try the following:
public void appendToFile(String content) {
try {
PrintStream outToFile = new PrintStream(new BufferedOutputStream(
new FileOutputStream(fileHandle.getPath(), true)));
Scanner in = new Scanner(content);
while (in.hasNextLine()) {
outToFile.println(in.nextLine());
}
in.close();
outToFile.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Unable to Write to File");
}
}
Add the following line to your code and see what is the default encoding used to write the file and post the result here, please.
System.out.println(System. getPropert y("file.en coding"));
System.out.println(System.
Add this line too:
System.out.println(Charset .defaultCh arset().di splayName( ));
System.out.println(Charset
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
HI
You need choose the encoding in Notepad as UTF-8, and then try to open it.
Notepad shows special characters if not encoded as UTF-8.
Kusheel
You need choose the encoding in Notepad as UTF-8, and then try to open it.
Notepad shows special characters if not encoded as UTF-8.
Kusheel
The issue is nothing to do with encoding - it's to do with linefeeds
ASKER
i removed the /r/n and now it's working. BTW is i don't use println then what line break code should i use. This solution should work for notepad.
depends on what platform you're going to be opening the file
if you're opening it on the same platform that you're java app is running you can use System.getProperty("line.s eparator")
if you're opening it on the same platform that you're java app is running you can use System.getProperty("line.s