A MORE SOPHISTICATED APPROACH TO JAVA OUTPUT


Output sofar has been achieved by directly linking the print and println methods to the predefined instance out (contained in the System class) of the PrintStream class. Thus:

System.out.print("Some output");

However, the print and println methods can also be found in the PrintWriter "stream" class. A class diagram indicating the relationship between this class and other output classes is given in Figure 1.

CLASS DIAGRAM SHOWUNG API CLASSES ASSOCIATED WITH OUTPUT

Fig 1: Class diagram showing API classes associated wuth output.

Thus we can use these methods by linking them to an appropriate PrintWriter object. The printWriter class has a number of constructors associated with it one of which is:

PrintWriter(OutputStream out, boolean autoFlush) 

which has two formal parameters; out which is of type OutputStream, and autoFlush which is of type Boolean. The second argument should be assigned the value true to ensure that the contents of the buffer are output directly to the screen when ever a print or println is encountered instead of being held in the buffer until there is no more temporary storage or a new line character is encountered (if we do not wish this to happen we should assign the value false) to the argument.

For the first argument we need an OutputStream object. Alternatively, from Figure 1, we can use a subclass object of OutputStream, such as an instance of the class PrintStream. As we now know an appropriate predefined PrintStream instance, called out, can be found in the System class. Thus we can create an instance of the class PrintWriter, which we will call screenOutput as follows:

PrintWriter screenOutput = mew PrintWriter(System.out,true);

Thus if we wish to output Halo World to the screen we might include the following statement in a piece of Java code:

screenOutput.print("Halo World")

or:

screenOutput.println("Halo World Again")

1. Example

// HALO WORLD PROGRAM 4
// Frans Coenen
// Monday 15 January 1999
// The University of Liverpool, UK

// Import packages containing predefined classes

import java.io.*;

class HelloWorld4 {

    // ------------------- FIELDS ------------------------               
        
    // Create BufferedReader class instance

    public static InputStreamReader input         = 
    		new InputStreamReader(System.in);
    public static BufferedReader    keyboardInput = 
    		new BufferedReader(input);

    // Crerste PrintWriter class instance

    static PrintWriter screenOutput = new PrintWriter(System.out,true);

    // ------------------ METHODS ------------------------
    
    /* Main method  */
    
    public static void main(String[] args) throws IOException {
    	String name;
	
	screenOutput.print("What is your name? ");
	screenOutput.flush(); 
	name = keyboardInput.readLine();
	
	screenOutput.print("\nHello " + name );
	screenOutput.println(" - Congratulations on writing a more" + 
		" sophisticated Java program that inovles output!\n\n");
        }
    }

The flush included in the above ensures that the output is directly passed to the screen and not temporarily held in the buffer.




Created and maintained by Frans Coenen. Last updated 02 October 2003