Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Carriage Return Line Feed for Java (vbCrLf) 1

Status
Not open for further replies.

adiMasher

Programmer
Aug 31, 2001
144
US
Hey Everyone.

This may seem a bit trivial but it's something I spent WAY TOO LONG trying to figure out.

in short, for all you VB programmers that are used to vbCrLf to take care of any new lines the combination equivilent is "\r\n".

For me this came into play when attempting to write to a log file and the only newline character I was giving it "\n". On a win32 system under notepad this was coming as a box character that did nothing.

Low and behold. I thought well maybe I need a carriage return "\r" like in VB. wah lah. new lines like grandpa used to make.

Martin I don't suffer from insanity.
I enjoy every minute of it.
 
Martin,

Your solution works fine provided that your code will run only on a Win32 system. In using this approach, however, you are sacrificing cross-platform compatibility.

While Win32 systems (and Win16 and DOS) use "\r\n" for a line separator, Unix uses only "\n" and Mac uses "\r" for line separators. Hard-coding "\r\n" for new lines will result in code that behaves unexpectedly on other platforms.

If there is a possibility of your code being run on different platforms at any point in the future, you'll want to let the JVM tell you which line separator to use. (My personal philosophy is to always strive for cross-platform compatibility in my code. It saves headaches later if/when I re-use code for other projects.)

The way you get this information in Java is by consulting the java.lang.System object. The System object provides a static method, getProperty, which can be used to get the line separator character(s) for the platform.

So instead of coding...
Code:
String newLine = "\r\n";
you use...
Code:
String newLine = System.getProperty("line.separator");

Then, whenever you would've used your hard-coded string for a new line, you use the newLine string and you'll still be cross-platform compatible.

The string "line.separator" is a key used to look up the desired information. System has several other properties which will be of interest also. Particularly, "file.separator" and "path.separator" will be of interest if you are interacting with the file system at all. (Also note that the getProperty method can throw some exceptions, so you'll want to consider putting the call in a try-catch block to handle any exceptions gracefully.)

Take a look at Sun's Java (J2SE) API documentation on their web site. The API for version 1.4.1 can be found at:
The specific page for the System object is at:
(I've got the API page in my browser's bookmarks/favorites because I use it ALL the time!)

Good luck!

Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top