Home arrow Java arrow Page 3 - Java Print Streams

Java Print Streams

The first two output streams that Java programmers encounter are usually instances of the java.io.Printstream class. If you want to learn more about print streams, keep reading; this is the first part of a three-part series on the topic. It is excerpted from chapter seven of Java I/O, Second Edition, written by Elliotte Rusty Harold (O'Reilly, 2006; ISBN: 0596527500). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 20
June 14, 2007
  1. · Java Print Streams
  2. · Print Versus Write
  3. · Line Breaks
  4. · Error Handling
  5. · Formatter

print this article

Java Print Streams - Line Breaks
(Page 3 of 5 )

As previously mentioned, the println() method always adds a line break at the end of each line it prints. You can even call println() with no arguments to print just a line break:

  public void println()

The line break character varies from platform to platform. In particular:

  1. On Unix (including Mac OS X), itís a linefeed,\n, ASCII 10.
  2. On Mac OS 9, itís a carriage return,\r, ASCII 13.
  3. On Windows, itís a carriage return linefeed pair,\r\n, ASCII 13 followed by ASCII 10.

This is almost never what you actually want!

Most file formats and most network protocols care a great deal about which line break character is written.* For instance, if youíre writing a web client or server, the HTTP specification requires that header lines end with carriage return linefeed pairs. It doesnít matter whether the client or server is a Mac, a PC, a Unix workstation, or a Palm Pilot. It must use\r\nas the line break. You can specify this by explicitly passing the line break you want to theprint()method rather than callingprintln(). For example:

  for (int i = 0; i <= 127; i++) {

In practice, most HTTP servers and clients accept requests that use the wrong line breaks. However, some arenít so forgiving, and you really shouldnít count on this behavior.

If for some reason you want to know which line break character will be used, theline.separatorsystem property will tell you:

  String lineBreak = System.getProperty("line.separator");

Not all line breaks are created equal. If thePrintStream is set toautoFlushóthat is, if the second argument to the constructor istrueóafter every call toprintln()and after every linefeed thatís printed, the underlying stream will be flushed. Thus,out.println()andout.print("\n")both flush the stream. So doesout.print("\r\n"), because it contains a linefeed. However,out.print("\r")does not cause an automatic flush.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials