SunQuest
 
       Java
  Home arrow Java arrow Page 5 - Java Print Streams
IBM developerWorks
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 
Moblin 
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 - Formatter


    (Page 5 of 5 )

     

    In fact, printf() is a little more general than System.out (though that’s its primary justification). Besides printf(), the PrintStreamclass also has aformat()method:

      public PrintStream format(String format, Object... args)

    This does exactly the same thing asprintf(). That is, the previous example could be rewritten like this and produce identical output:

      for (double degrees = 0.0; degrees < 360.0; degrees++) {
       
    double radians = Math.PI * degrees / 180.0;
       
    double grads = 400 * degrees / 360;
       
    System.out.format("%5.1f %5.1f %5.1f\n", degrees , radians, grads);
     
    }

    Why two methods, then? Theformat()method is used not just byPrintStreambut also by thejava.util.Formatterclass:

      public class Formatter implements Flushable, Closeable 

    printf()is there solely to make C programmers feel nostalgic.

    Formatteris the object-oriented equivalent ofsprintf()andfprintf()in C. Rather than writing its output onto the console, it writes it into a string, a file, or an output stream. Pass the object you want to write into to theFormatterconstructor. For example, this code fragment creates aFormatterthat writes data into a file named angles.txt:

      Formatter formatter = new Formatter("angles.txt");

    Once you’ve created aFormatterobject, you can write to it using theformat()method just as you would withSystem.out.format(), except that the output goes into the file rather than onto the console:

      for (double degrees = 0.0; degrees < 360.0; degrees++) {
       
    double radians = Math.PI * degrees / 180.0;
       
    double grads = 400 * degrees / 360;
       
    formatter.format("%5.1f %5.1f %5.1f\n", degrees , radians, grads);
      }

    Formatters are not output streams, but they can and should be flushed and closed just the same:

      formatter.flush();
     
    formatter.close();

    Please check back next week for the continuation of this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 6 hosted by Hostway