All Methods
zio package

context

zio
Class Output


java.lang.Object

  extended byzio.Output


public class Output
extends java.lang.Object

Provides for text output to files, command windows, sockets, character arrays, Strings, and File objects.

Use constructor with file name for file output and without file name for System.out.

Quick Usage:

 // This example outputs an int, a char, a double, and a String on the
 // next line of the command window.
    Output outc = new Output();  
    outc.writeInt(2);
    outc.writeChar(' ');
    outc.writeDouble(2.2);
    outc.writeString(" -- that's all folks!");
    outc.writeEndOfLine();
 // next example copies a file, line by line from "infile.txt" to 
 // "outfile.txt" using the Input class with the Output class
     Input in = new Input("infile.txt");
     Output out = new Output("outfile.txt");
     for(;;) {
         String line = in.readLine();
         if( in.eof() ) break;
         out.writeLine(line);
     }
     in.close(); out.close();
 

Of the write... methods, only the writeLine method is guaranteed to have finished writing when execution moves on to something else. This may be a significant fact for you if your program terminates abnormally and you are trying to figure out what managed to finish before your program got into trouble.

Flushing the output buffer is not automatic. It happens only with these methods: writeLine, flush, and close.

Error behavior can be changed by subclassing the Zio class.

Consider using the File class in conjunction with this class; that way offers greater flexibility over the direct use of file names.

Version:
Jul 22, 2005
Author:
copyright 2005 by J Adrian Zimmer
Licensed under the Open Software License version 2.1

Constructor Summary
Output()
          Creates an Output object capable of writing to the command window.
Output(java.io.File file)
          Creates an Output object from any a File object.
Output(java.io.OutputStream stream)
          Creates an Ouput object from any OutputStream object.
Output(java.lang.String fileName)
          Creates an Output object capable of writing to the named file.
Output(java.io.Writer writer)
          Creates an Output object from any writer object.
 
Method Summary
static void appendNextOpenFile()
          Makes the next file opening constructor create an object that will append to an existing file.
 void close()
          Tells operating system your program is finished writing.
 void flush()
          Flush the output buffer without bothering with writeLine.
 void writeChar(char writeMe)
          Writes a character
 void writeDouble(double writeMe)
          Writes a double.
 void writeDouble(double writeMe, int fractionalDigits, int size)
          Writes a double in decimal form within a specified number of columns and using a specified number of digits to the right of the decimal point.
 void writeInt(int writeMe)
          Writes an int.
 void writeInt(int writeMe, int size)
          Writes an int as a decimal number within a specified number of columns.
 void writeLine()
          Writes the character code(s) that signal the end of a line.
 void writeLine(java.lang.String line)
          Writes a String and then writes the character code(s) that signals the end of a line.
 void writeString(java.lang.String writeMe)
          Writes a String.
 void writeString(java.lang.String writeMe, int size)
          Writes a String within a specified number of columns.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Output


public Output()
Creates an Output object capable of writing to the command window.


Output


public Output(java.lang.String fileName)
Creates an Output object capable of writing to the named file. If file already exists, it will be cleared of content first.

Parameters:
fileName - the name of the file to be written on

Output


public Output(java.io.File file)
Creates an Output object from any a File object.


Output


public Output(java.io.Writer writer)
Creates an Output object from any writer object. Writer objects include StringWriters and CharArrayWriters.


Output


public Output(java.io.OutputStream stream)
Creates an Ouput object from any OutputStream object. Use this constructor with sock.getInputStream() to perform output to a Socket object.

Parameters:
stream - any OutputStream object
Method Detail

writeChar


public final void writeChar(char writeMe)
Writes a character

Parameters:
writeMe - the char to be written

writeString


public final void writeString(java.lang.String writeMe)
Writes a String. A "\n" within the String will cause a new line of output to be begun. However, unlike with System.out.print, the output buffer will not be flushed. That will wait until close() or writeLine() has executed.

Parameters:
writeMe - the String to be written

writeString


public final void writeString(java.lang.String writeMe,
                              int size)
Writes a String within a specified number of columns. The string will be truncated or padded with blanks as necessary. The size parameter may be negative, in which case a string which does not require all -size columnns will be right justified within its allotted size. Normally a string is left justified within its allotted size.

Parameters:
writeMe - the String to be written
size - the size of the output String

writeLine


public final void writeLine()
Writes the character code(s) that signal the end of a line.

Flushes the output buffer.


writeLine


public final void writeLine(java.lang.String line)
Writes a String and then writes the character code(s) that signals the end of a line.

Flushes the output buffer.

Parameters:
line - the string to be written as a line

writeInt


public final void writeInt(int writeMe)
Writes an int.

Parameters:
writeMe - The int to be converted to String form and written.

writeDouble


public final void writeDouble(double writeMe)
Writes a double.

Parameters:
writeMe - The double to be converted to String form and written.

writeInt


public final void writeInt(int writeMe,
                           int size)
Writes an int as a decimal number within a specified number of columns. The size parameter may be negative, in which case a number which does not require all -size columnns will be left justified within its allotted size. Normally the number is right justified within its allotted size.

The size parameter must allow for at least two columns. If the number does not fit into its allotted space, it will be replaced with two exclamation marks.

Parameters:
writeMe - the int to be written
size - the number of characters actually output

writeDouble


public final void writeDouble(double writeMe,
                              int fractionalDigits,
                              int size)
Writes a double in decimal form within a specified number of columns and using a specified number of digits to the right of the decimal point. The size parameter may be negative, in which case a number which does not require all -size columns will be left justified within its allotted size. Normally the number is right justified within its allotted size.

The size parameter must allow for at least two columns. If the number does not fit into its allotted space, it will be replaced with two exclamation marks.

Parameters:
writeMe - the double to be converted to String form and written
fractionalDigits - number of fractional digits desired
size - the number of characters actually written

close


public final void close()
Tells operating system your program is finished writing. Always close files when you are done with them. Files can be reopened with by instantiating another Input or Output object.


appendNextOpenFile


public static final void appendNextOpenFile()
Makes the next file opening constructor create an object that will append to an existing file. Applies only to the next use of new Output(String fileName) or new Output(File file) .


flush


public final void flush()
Flush the output buffer without bothering with writeLine.


author
context