Solved

Newline character in txt file(notepad) displayed as square character

Posted on 2011-02-25
17
847 Views
Last Modified: 2012-06-27
	
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");
		}
	}

Open in new window


 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.

                  
0
Comment
Question by:PearlJamFanatic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
  • +2
17 Comments
 
LVL 11

Expert Comment

by:dougaug
ID: 34978694
In which operating system are you running the code above?
0
 

Author Comment

by:PearlJamFanatic
ID: 34978695
windows xp
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34978699
>>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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 34978705
Worthwhile attaching (not pasting) the text file in question if you can
0
 

Author Comment

by:PearlJamFanatic
ID: 34978714
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.
0
 

Author Comment

by:PearlJamFanatic
ID: 34978719
attached.
result.txt
0
 
LVL 11

Expert Comment

by:dougaug
ID: 34978725
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34978747
I suspect 'content' has lone newlines ( '\n' ) embedded in it
0
 

Author Comment

by:PearlJamFanatic
ID: 34978760
there was one lone \n instance which has been corrected now. The problem still persists however.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34978768
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");
		}
	}

Open in new window

0
 
LVL 11

Expert Comment

by:dougaug
ID: 34978780
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.getProperty("file.encoding"));
0
 
LVL 11

Expert Comment

by:dougaug
ID: 34978792
Add this line too:

System.out.println(Charset.defaultCharset().displayName());
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 34978919
>                   outToFile.println(content);

your using the platform default line ending here

>  appendToFile("blah blah blah \r\n");

And your hardcoding the end of line and adding an extra one
you need to do one or the other

eg. use the following if you're hard coding

outToFile.print(content);

or to use platform default use:

 appendToFile("blah blah blah");
0
 

Expert Comment

by:kusheelkumar
ID: 34979060
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34979089
The issue is nothing to do with encoding - it's to do with linefeeds
0
 

Author Comment

by:PearlJamFanatic
ID: 34994254
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 34994485
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.separator")
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question