Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Print and display problem  RSS feed

 
michael yue
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

I have a problem with writing to text file with .txt extension. Wherever i write to it, it print out a new line as a some sort of square character. This only happen when I open the file with notepad but when I open in wordpad it display in normal way meaning it print a new line instead of the character. Is there any way out of this?
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rollin Park, your display name does not comply with our naming policy, so I'll have to ask you to click the My Profile link next to the moose head, go to "View/Update Profile", and change the "Publicly Displayed Name" field into something compliant with the policy.
As for your question, the end-of-line marker varies from platform to platform. Windows uses "/n/r", Unix '/n' and Mac '/r'. Since Java is designed to be cross platform one can obtain proper marker in a couple of different ways:
System.getProperties("line.separator") will return it as a String. java.io.PrintWriter.println() will append the proper separator with each call.
java.io.BufferedWriter.newLine() will write a line separator directly to the file.
Use whatever method fits best with your requirements.
 
Shabbir Anjum
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
write file as

"abc.txt"
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shabbir anjum:
write file as
"abc.txt"


That's what Rollin is doing. The extension of a file has no impact on what content a file has. Taking a .txt file and naming it .mp3 does not turn it into an audio file. The problem here is that windows programs like Notepad expect an end-of-line marker of "\n\r" and he's writing "\n".
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't imagine any good reason for using notepad.
scite, for instance, is a powerful, fast and lean editor, freely available, OpenSource, syntax highlightening, ... and flexible in encoding.

I don't know why Microsoft continues selling it - it must be written by Bill Gates himself...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!