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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date