Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Apr 21, 1999

Cross-Platform Line Separators

When your application generates multiple lines of output and is responsible for manually separating those lines, you may be inclined to insert an instance of the linefeed character (usually represented as "\n"). For example:
 
java.io.Writer wr;
.
.
.
wr.write("First line");
wr.write("\n");
wr.write("Second line");
While this code may work for many applications on some platforms, it is not portable code. That's because different platforms traditionally use different line separators. For example, most Windows applications expect a carriage return/linefeed combination to mark the beginning of a new line. However, other platforms may only expect the carriage return character.

To ensure that your Java code will generate the appropriate character for the platform that it's running on, use the System.getProperty() method to obtain the line separator dynamically as follows:

 
java.io.Writer wr;
.
.
.
wr.write("First line");
wr.write(System.getProperty("line.separator"));
wr.write("Second line");
Brett Spell
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date