type.lang
Class UniWriter

java.lang.Object
  extended by type.lang.UniWriter

public class UniWriter
extends java.lang.Object

A universal writer class that provides a consistent interface for writing text to disk files or to Standard Output. Standard Output is the window from which the application was launched (the command/terminal window or UniCon).

To use its services, perform the following steps in the shown order:

  1. Select a constructor, pass the appropriate output target to it, and create an instance.
  2. Invoke, on the instance, one of the print or println methods to write text to the target.
  3. Repeat the above step to write additional text elements as needed.
  4. Invoke, on the instance, the close() method.
Note: The only difference between print and println is that println outputs an End-Of-Line (EOL) marker after outputting the data. This marker is platform-dependent so this class communicate with the O/S on which it is running in order to automatically use the correct marker. Under Unix, the marker is '\n' and is '\r\n' under Windows.

Example -1:
Suppose we want to create a text file "Text" containing the 3-letter names of the weekdays (excluding weekends):

 import type.lang.*;
 public class WriteText
 {  public static void main (String[] args)
    {  final String WEEKDAYS = "MonTueWedThuFri";
       final int SIZE = 3;
       //--------------------------------Open the File:
       UniWriter uw = new UniWriter("Text");
       //--------------------------------Write data:
       for (int num = 0; num < WEEKDAYS.length() / SIZE; num++)
       {  int start = num * SIZE;
          String day = WEEKDAYS.substring(start, start + SIZE);
          uw.println(day);
       }
       //-------------------------------Close the File:
       uw.close();
    }
 }

Example -2:
Suppose we want to create a text file "Mixed" containing the square roots of all integers between 1 and 10. Each record would contain an integer and its square root, separated by a space:

 import type.lang.*;
 public class WriteMixed
 {  public static void main (String[] args)
    {  final int RECORDS = 10;
       //--------------------------------Open the File:
       UniWriter uw = new UniWriter("Mixed");
       //--------------------------------Write data:
       for (int num = 1; num <= RECORDS; num++)
       {  uw.print(num);
          uw.print(" ");
          uw.println(Math.sqrt(num));
       }
       //-------------------------------Close the File:
       uw.close();
    }
 }
The content of the generated file is shown below:
        1 1.0
        2 1.4142135623730951
        3 1.7320508075688772
        4 2.0
        5 2.23606797749979
        6 2.449489742783178
        7 2.6457513110645907
        8 2.8284271247461903
        9 3.0
        10 3.1622776601683795
To obtain a nicer-looking output, we can format the numbers by adding format descriptors. See the IO class for details.

Version:
3.0 (Summer 2000), 3.5 (Summer 2001), 4.0 (Summer 2002)
Author:
H. Roumani, roumani@cs.yorku.ca

Constructor Summary
UniWriter()
          Create an instance connected to Standard Output.
UniWriter(java.lang.String target)
          Create an instance connected to the passed target such that subsequent output will overwrite any content already in it.
UniWriter(java.lang.String target, boolean append)
          Create an instance connected to the passed target and specify whether subsequent output should append or replace existing content, if any.
 
Method Summary
 void close()
          Remove the connection between the target and this instance (thereby freeing any reserved resources).
 void print(boolean value)
          Output to the target connected to this instance the literal true if the passed value is true and output false otherwise.
 void print(boolean value, java.lang.String fd)
          Output to the target connected to this instance the literal true if the passed value is true and output false otherwise.
 void print(byte value, java.lang.String fd)
          Output the passed value to the target connected to this instance using the passed format descriptor.
 void print(char value)
          Output the passed value to the target connected to this instance.
 void print(char value, java.lang.String fd)
          Output the passed value to the target connected to this instance using the passed format descriptor.
 void print(double value)
          Output the passed value to the target connected to this instance.
 void print(double value, java.lang.String fd)
          Output the passed value to the target connected to this instance using the passed format descriptor.
 void print(float value, java.lang.String fd)
          Output the passed value to the target connected to this instance using the passed format descriptor.
 void print(int value, java.lang.String fd)
          Output the passed value to the target connected to this instance using the passed format descriptor.
 void print(long value)
          Output the passed value to the target connected to this instance.
 void print(long value, java.lang.String fd)
          Output the passed value to the target connected to this instance using the passed format descriptor.
 void print(java.lang.Object value)
          Output the string representation of the passed object reference to the target connected to this instance.
 void print(java.lang.Object value, java.lang.String fd)
          Output the string representation of the passed object reference to the target connected to this instance using the passed format descriptor.
 void print(short value, java.lang.String fd)
          Output the passed value to the target connected to this instance using the passed format descriptor.
 void println()
          Output the End-Of-Line marker.
 void println(boolean value)
          Output to the target connected to this instance the literal true if the passed value is true and output false otherwise.
 void println(boolean value, java.lang.String fd)
          Output to the target connected to this instance the literal true if the passed value is true and output false otherwise.
 void println(byte value, java.lang.String fd)
          Output the passed value to the target connected to this instance using the passed format descriptor and followed by an End-Of-Line.
 void println(char value)
          Output the passed value to the target connected to this instance followed by an End-Of-Line.
 void println(char value, java.lang.String fd)
          Output the passed value to the target connected to this instance using the passed format descriptor and followed by an End-Of-Line.
 void println(double value)
          Output the passed value to the target connected to this instance followed by an End-Of-Line.
 void println(double value, java.lang.String fd)
          Output the passed value to the target connected to this instance using the passed format descriptor and followed by an End-Of-Line.
 void println(float value, java.lang.String fd)
          Output the passed value to the target connected to this instance using the passed format descriptor and followed by an End-Of-Line.
 void println(int value, java.lang.String fd)
          Output the passed value to the target connected to this instance using the passed format descriptor and followed by an End-Of-Line.
 void println(long value)
          Output the passed value to the target connected to this instance followed by an End-Of-Line.
 void println(long value, java.lang.String fd)
          Output the passed value to the target connected to this instance using the passed format descriptor and followed by an End-Of-Line.
 void println(java.lang.Object value)
          Output the string representation (as returned from toString()) of the passed object reference to the target connected to this instance.
 void println(java.lang.Object value, java.lang.String fd)
          Output the string representation of the passed object reference to target connected to this instance using the passed format descriptor and followed by an End-Of-Line.
 void println(short value, java.lang.String fd)
          Output the passed value to the target connected to this instance using the passed format descriptor and followed by an End-Of-Line.
 java.lang.String toString()
          Return a string representation of this instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

UniWriter

public UniWriter(java.lang.String target,
                 boolean append)
Create an instance connected to the passed target and specify whether subsequent output should append or replace existing content, if any.

Parameters:
target - can be:
  • The name [and path] of a disk file. Use '/' in the path under Unix and '\' under Windows. Note that you will need two backslashes '\\' if the path is a string literal. If the embedded path is relative then it is considered relative to the working directory specified in IO.workingDir. If the file already exists, and the passed append is false, then the file will removed and then recreated; i.e. it will be overwritten.
  • The null string. In this case, subsequent output will be directed to Standard Output; the window from which the application was launched.
append - a flag that, if true, indicates that subsequent output is to append (rather than overwrite) existing content in the target (if any). Otherwise, the target will be deleted, if present, and then recreated.
Throws:
java.lang.RuntimeException - with message: File cannot be opened for writing! if the file cannot be written to; e.g. write-protected.

UniWriter

public UniWriter(java.lang.String target)
Create an instance connected to the passed target such that subsequent output will overwrite any content already in it. This convenience constructor has the same effect as invoking the 2-argument constructor with append=false.

Parameters:
target - see the two-argument constructor.

UniWriter

public UniWriter()
Create an instance connected to Standard Output. This convenience constructor has the same effect as passing null to the 1-argument constructor.

Method Detail

toString

public java.lang.String toString()
Return a string representation of this instance.

Overrides:
toString in class java.lang.Object
Returns:
the string "A UniWriter connected to: " followed by the target on which the instance was constructed.

close

public void close()
Remove the connection between the target and this instance (thereby freeing any reserved resources).


println

public void println()
Output the End-Of-Line marker. This is typically used to output an empty line.


print

public void print(char value)
Output the passed value to the target connected to this instance. No trailing End-Of-Line is printed.

Parameters:
value - the value to be printed.

println

public void println(char value)
Output the passed value to the target connected to this instance followed by an End-Of-Line.

Parameters:
value - the value to be printed.

print

public void print(long value)
Output the passed value to the target connected to this instance. No trailing End-Of-Line is printed.

Parameters:
value - the value to be printed (its type can be byte, short, int or long).

println

public void println(long value)
Output the passed value to the target connected to this instance followed by an End-Of-Line.

Parameters:
value - the value to be printed (its type can be byte, short, int or long).

print

public void print(double value)
Output the passed value to the target connected to this instance. No trailing End-Of-Line is printed.

Parameters:
value - the value to be printed (its type can be float or double).

println

public void println(double value)
Output the passed value to the target connected to this instance followed by an End-Of-Line.

Parameters:
value - the value to be printed (its type can be float or double).

print

public void print(boolean value)
Output to the target connected to this instance the literal true if the passed value is true and output false otherwise. No trailing End-Of-Line is printed.

Parameters:
value - the value to be printed.

println

public void println(boolean value)
Output to the target connected to this instance the literal true if the passed value is true and output false otherwise. The output is followed by an End-Of-Line.

Parameters:
value - the value to be printed.

print

public void print(java.lang.Object value)
Output the string representation of the passed object reference to the target connected to this instance. If the reference is null, the string "" is printed. No trailing End-Of-Line is printed.

Parameters:
value - the value to be printed.

println

public void println(java.lang.Object value)
Output the string representation (as returned from toString()) of the passed object reference to the target connected to this instance. If the reference is null, the string "" is printed. The output is followed by an End-Of-Line.

Parameters:
value - the value to be printed.

print

public void print(byte value,
                  java.lang.String fd)
Output the passed value to the target connected to this instance using the passed format descriptor. No trailing End-Of-Line is printed.

Parameters:
value - the value to be printed.
fd - the format descriptor. (details)
See Also:
format

println

public void println(byte value,
                    java.lang.String fd)
Output the passed value to the target connected to this instance using the passed format descriptor and followed by an End-Of-Line.

Parameters:
value - the value to be printed.
fd - the format descriptor. (details)
See Also:
format

print

public void print(short value,
                  java.lang.String fd)
Output the passed value to the target connected to this instance using the passed format descriptor. No trailing End-Of-Line is printed.

Parameters:
value - the value to be printed.
fd - the format descriptor. (details)
See Also:
format

println

public void println(short value,
                    java.lang.String fd)
Output the passed value to the target connected to this instance using the passed format descriptor and followed by an End-Of-Line.

Parameters:
value - the value to be printed.
fd - the format descriptor. (details)
See Also:
format

print

public void print(char value,
                  java.lang.String fd)
Output the passed value to the target connected to this instance using the passed format descriptor. No trailing End-Of-Line is printed.

Parameters:
value - the value to be printed.
fd - the format descriptor. (details)
See Also:
format

println

public void println(char value,
                    java.lang.String fd)
Output the passed value to the target connected to this instance using the passed format descriptor and followed by an End-Of-Line.

Parameters:
value - the value to be printed.
fd - the format descriptor. (details)
See Also:
format

print

public void print(int value,
                  java.lang.String fd)
Output the passed value to the target connected to this instance using the passed format descriptor. No trailing End-Of-Line is printed.

Parameters:
value - the value to be printed.
fd - the format descriptor. (details)
See Also:
format

println

public void println(int value,
                    java.lang.String fd)
Output the passed value to the target connected to this instance using the passed format descriptor and followed by an End-Of-Line.

Parameters:
value - the value to be printed.
fd - the format descriptor. (details)
See Also:
format

print

public void print(long value,
                  java.lang.String fd)
Output the passed value to the target connected to this instance using the passed format descriptor. No trailing End-Of-Line is printed.

Parameters:
value - the value to be printed.
fd - the format descriptor. (details)
See Also:
format

println

public void println(long value,
                    java.lang.String fd)
Output the passed value to the target connected to this instance using the passed format descriptor and followed by an End-Of-Line.

Parameters:
value - the value to be printed.
fd - the format descriptor. (details)
See Also:
format

print

public void print(float value,
                  java.lang.String fd)
Output the passed value to the target connected to this instance using the passed format descriptor. No trailing End-Of-Line is printed.

Parameters:
value - the value to be printed.
fd - the format descriptor. (details)
See Also:
format

println

public void println(float value,
                    java.lang.String fd)
Output the passed value to the target connected to this instance using the passed format descriptor and followed by an End-Of-Line.

Parameters:
value - the value to be printed.
fd - the format descriptor. (details)
See Also:
format

print

public void print(double value,
                  java.lang.String fd)
Output the passed value to the target connected to this instance using the passed format descriptor. No trailing End-Of-Line is printed.

Parameters:
value - the value to be printed.
fd - the format descriptor. (details)
See Also:
format

println

public void println(double value,
                    java.lang.String fd)
Output the passed value to the target connected to this instance using the passed format descriptor and followed by an End-Of-Line.

Parameters:
value - the value to be printed (its type can be float or double).
fd - the format descriptor. (details)
See Also:
format

print

public void print(boolean value,
                  java.lang.String fd)
Output to the target connected to this instance the literal true if the passed value is true and output false otherwise. No trailing End-Of-Line is printed. The output is governed by the passed format descriptor.

Parameters:
value - the value to be printed.
fd - the format descriptor. (details)
See Also:
format

println

public void println(boolean value,
                    java.lang.String fd)
Output to the target connected to this instance the literal true if the passed value is true and output false otherwise. No trailing End-Of-Line character is printed. The output is governed by the passed format descriptor and is followed by an End-Of-Line.

Parameters:
value - the value to be printed.
fd - the format descriptor. (details)
See Also:
format

print

public void print(java.lang.Object value,
                  java.lang.String fd)
Output the string representation of the passed object reference to the target connected to this instance using the passed format descriptor. No trailing End-Of-Line is printed.

Parameters:
value - the value to be printed.
fd - the format descriptor. (details)
See Also:
format

println

public void println(java.lang.Object value,
                    java.lang.String fd)
Output the string representation of the passed object reference to target connected to this instance using the passed format descriptor and followed by an End-Of-Line.

Parameters:
value - the value to be printed.
fd - the format descriptor. (details)
See Also:
format


Java by Abstraction: A Client-View Approach