Home : Course Map : Chapter 9 : Java :
Text Output to Console
JavaTech
Course Map
Chapter 9

Introduction
Overview
Streams
Wrappers,Buffers
Console I/O
  Text Output 
     Demo 1

  Formatter/printf()
     Demo 2

  Tex 2t Input
     Demo 3

  Scanner
     
Demo 4
File Class
  File I/O
  File Output-Text
     Demo 5

  Formatter to File
     Demo 6

  File Input - Text
    Demo 7

  Scanner - Files
     Demo 8

  File I/O - Binary
     Demo 9
   Demo 10
File Chooser Dialog
  Demo 11

Character Codes
  Demo 12
   Demo13
Object I/O
Types to Bytes
Stream Filters
Other I/O Topics
Exercises

    Supplements
Character I/O
  Demo 1   Demo 2
Random Access
  Demo 3
ZIP/GZIP Streams
  Demo 4
Piped Streams
  Demo 5
NIO Framework
More NIO
  Demo 6

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The program PrintWriterApp, shown below, demonstrates string output, primitive type data output, and the wrapping of streams to gain greater capabilities. We first wrap the System.out object, which is an 8-bit stream, with an OutputStreamWriter to obtain 16-bit streaming.

However, the OutputStreamWriter class (see specifications) provides only a few methods. So we wrap our OutputStreamWriter object with PrintWriter, which offers a full set of print() and println() methods for printing strings and primitive types.

import java.io.*;

/**
  * Demonstrate stream wrapping by writing text to the
  * console using the standard output from System.out
  * wrapped with OutputStreamWriter and PrintWriter.
**/
public class PrintWriterApp
{
  public static void main (String arg[]) {

    // The System.out standard output stream is already opened by default.
    // Wrap in a new writer stream to obtain 16 bit capability.
    OutputStreamWriter writer = new OutputStreamWriter (System.out);

    // In turn, wrap the PrintWriter stream around this output stream
    // and use the second argument in the constructor to turn
    // on the autoflush.
    PrintWriter print_writer = new PrintWriter (writer,true);

    // PrintWriter does autoflushing and offers a println
    // method that includes line return.
    print_writer.println ("Text output with PrintWriter.");
    print_writer.println ("Primitives converted to strings:");

    boolean a_boolean =  false;
    byte    a_byte    =  114;
    short   a_short   =  1211;
    int     an_int    =  1234567;
    long    a_long    =  987654321;
    float   a_float   =  983.6f;
    double  a_double  = -4.297e-15;

    print_writer.println (a_boolean);
    print_writer.println (a_byte);
    print_writer.println (a_short);
    print_writer.println (an_int);
    print_writer.println (a_long);
    print_writer.println (a_float);
    print_writer.println (a_double);


    // PrintWriter doesn't throw IOExceptions but instead
    // offers the catchError() method
    if( print_writer.checkError ()) {
        System.out.println ("An output error occurred!" );
    }
  } // main

}// class PrintWriterApp

The output from this class goes as

Text output with PrintWriter.
Primitives converted to strings:
false
114
1211
1234567
987654321
983.6
-4.297E-15

Note that some wrapper stream classes include a buffer that collects data until it is full and then the whole buffer data set is sent to its destination in one operation. This is more efficient than sending each byte one at a time because of the overhead involved in any transfer.

To insure that data does not remain in a partially filled buffer, you may need to invoke a flush method . With both PrintStream and PrintWriter, you have the option of turning on an auto-flushing switch.

References & Web Resources

 

Latest update: Nov. 11, 2004

              Tech
Histogram I/O
Hist I/O - Get/Set
  Demo 1
Hist I/O - Objects
  Demo 2
HistogramStream
  Demo 3
Filtering Data
  Demo 4
Exercises

           Physics
Physics Model
Simulation Design
Physics Simulator
  Demo 1
Experiment Design
Experiment Sim.
  Demo 2
Analysis
Expt. + Analysis
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.