Java
  Home arrow Java arrow Page 3 - Java Print Streams
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Java Print Streams
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-06-14

    Table of Contents:
  • Java Print Streams
  • Print Versus Write
  • Line Breaks
  • Error Handling
  • Formatter

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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++) {
       
    out.print(i);
       
    out.print("\r\n");
     
    }

    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.

    More Java Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Java I/O, Second Edition," published by...
     

    Buy this book now. This article is excerpted from chapter seven of Java I/O, Second Edition, written by Elliotte Rusty Harold (O'Reilly, 2006; ISBN: 0596527500). Check it out today at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway