Home arrow Java arrow Page 5 - 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 - 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:


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.

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-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials