FileDocCategorySizeDatePackage
PrintStream.javaAPI DocAndroid 1.5 API28375Wed May 06 22:41:04 BST 2009java.io

PrintStream

public class PrintStream extends FilterOutputStream implements Appendable, Closeable
Wraps an existing {@link OutputStream} and provides convenience methods for writing common data types in a human readable format. This is not to be confused with DataOutputStream which is used for encoding common data types so that they can be read back in. No {@code IOException} is thrown by this class. Instead, callers should use {@link #checkError()} to see if a problem has occurred in this stream.
since
Android 1.0

Fields Summary
private static final String
TOKEN_NULL
private boolean
ioError
indicates whether or not this PrintStream has incurred an error.
private boolean
autoflush
indicates whether or not this PrintStream should flush its contents after printing a new line.
private String
encoding
private final String
lineSeparator
Constructors Summary
public PrintStream(OutputStream out)
Constructs a new {@code PrintStream} with {@code out} as its target stream. By default, the new print stream does not automatically flush its contents to the target stream when a newline is encountered.

param
out the target output stream.
throws
NullPointerException if {@code out} is {@code null}.
since
Android 1.0

 //$NON-NLS-1$

    // private Formatter formatter;

                                                                                   
       
        super(out);
        if (out == null) {
            throw new NullPointerException();
        }
    
public PrintStream(OutputStream out, boolean autoflush)
Constructs a new {@code PrintStream} with {@code out} as its target stream. The parameter {@code autoflush} determines if the print stream automatically flushes its contents to the target stream when a newline is encountered.

param
out the target output stream.
param
autoflush indicates whether to flush contents upon encountering a newline sequence.
throws
NullPointerException if {@code out} is {@code null}.
since
Android 1.0

        super(out);
        if (out == null) {
            throw new NullPointerException();
        }
        this.autoflush = autoflush;
    
public PrintStream(OutputStream out, boolean autoflush, String enc)
Constructs a new {@code PrintStream} with {@code out} as its target stream and using the character encoding {@code enc} while writing. The parameter {@code autoflush} determines if the print stream automatically flushes its contents to the target stream when a newline is encountered.

param
out the target output stream.
param
autoflush indicates whether or not to flush contents upon encountering a newline sequence.
param
enc the non-null string describing the desired character encoding.
throws
NullPointerException if {@code out} or {@code enc} are {@code null}.
throws
UnsupportedEncodingException if the encoding specified by {@code enc} is not supported.
since
Android 1.0

        super(out);
        if (out == null || enc == null) {
            throw new NullPointerException();
        }
        this.autoflush = autoflush;
        if (!Charset.isSupported(enc)) {
            throw new UnsupportedEncodingException(enc);
        }
        encoding = enc;
    
public PrintStream(File file)
Constructs a new {@code PrintStream} with {@code file} as its target. The virtual machine's default character set is used for character encoding.

param
file the target file. If the file already exists, its contents are removed, otherwise a new file is created.
throws
FileNotFoundException if an error occurs while opening or creating the target file.
throws
SecurityException if a security manager exists and it denies writing to the target file.
since
Android 1.0

        super(new FileOutputStream(file));
    
public PrintStream(File file, String csn)
Constructs a new {@code PrintStream} with {@code file} as its target. The character set named {@code csn} is used for character encoding.

param
file the target file. If the file already exists, its contents are removed, otherwise a new file is created.
param
csn the name of the character set used for character encoding.
throws
FileNotFoundException if an error occurs while opening or creating the target file.
throws
NullPointerException if {@code csn} is {@code null}.
throws
SecurityException if a security manager exists and it denies writing to the target file.
throws
UnsupportedEncodingException if the encoding specified by {@code csn} is not supported.
since
Android 1.0

        super(new FileOutputStream(file));
        if (csn == null) {
            throw new NullPointerException();
        }
        if (!Charset.isSupported(csn)) {
            throw new UnsupportedEncodingException();
        }
        encoding = csn;
    
public PrintStream(String fileName)
Constructs a new {@code PrintStream} with the file identified by {@code fileName} as its target. The virtual machine's default character set is used for character encoding.

param
fileName the target file's name. If the file already exists, its contents are removed, otherwise a new file is created.
throws
FileNotFoundException if an error occurs while opening or creating the target file.
throws
SecurityException if a security manager exists and it denies writing to the target file.
since
Android 1.0

        this(new File(fileName));
    
public PrintStream(String fileName, String csn)
Constructs a new {@code PrintStream} with the file identified by {@code fileName} as its target. The character set named {@code csn} is used for character encoding.

param
fileName the target file's name. If the file already exists, its contents are removed, otherwise a new file is created.
param
csn the name of the character set used for character encoding.
throws
FileNotFoundException if an error occurs while opening or creating the target file.
throws
NullPointerException if {@code csn} is {@code null}.
throws
SecurityException if a security manager exists and it denies writing to the target file.
throws
UnsupportedEncodingException if the encoding specified by {@code csn} is not supported.
since
Android 1.0

        this(new File(fileName), csn);
    
Methods Summary
public java.io.PrintStreamappend(char c)
Appends the character {@code c} to the target stream. This method works the same way as {@link #print(char)}.

param
c the character to append to the target stream.
return
this stream.
since
Android 1.0

        print(c);
        return this;
    
public java.io.PrintStreamappend(java.lang.CharSequence csq)
Appends the character sequence {@code csq} to the target stream. This method works the same way as {@code PrintStream.print(csq.toString())}. If {@code csq} is {@code null}, then the string "null" is written to the target stream.

param
csq the character sequence appended to the target stream.
return
this stream.
since
Android 1.0

        if (null == csq) {
            print(TOKEN_NULL);
        } else {
            print(csq.toString());
        }
        return this;
    
public java.io.PrintStreamappend(java.lang.CharSequence csq, int start, int end)
Appends a subsequence of the character sequence {@code csq} to the target stream. This method works the same way as {@code PrintStream.print(csq.subsequence(start, end).toString())}. If {@code csq} is {@code null}, then the specified subsequence of the string "null" will be written to the target stream.

param
csq the character sequence appended to the target stream.
param
start the index of the first char in the character sequence appended to the target stream.
param
end the index of the character following the last character of the subsequence appended to the target stream.
return
this stream.
throws
IndexOutOfBoundsException if {@code start > end}, {@code start < 0}, {@code end < 0} or either {@code start} or {@code end} are greater or equal than the length of {@code csq}.
since
Android 1.0

        if (null == csq) {
            print(TOKEN_NULL.substring(start, end));
        } else {
            print(csq.subSequence(start, end).toString());
        }
        return this;
    
public booleancheckError()
Flushes this stream and returns the value of the error flag.

return
{@code true} if either an {@code IOException} has been thrown previously or if {@code setError()} has been called; {@code false} otherwise.
see
#setError()
since
Android 1.0

        if (out != null) {
            flush();
        }
        return ioError;
    
public synchronized voidclose()
Closes this print stream. Flushes this stream and then closes the target stream. If an I/O error occurs, this stream's error state is set to {@code true}.

since
Android 1.0

        flush();
        if (out != null) {
            try {
                out.close();
                out = null;
            } catch (IOException e) {
                setError();
            }
        }
    
public synchronized voidflush()
Ensures that all pending data is sent out to the target stream. It also flushes the target stream. If an I/O error occurs, this stream's error state is set to {@code true}.

since
Android 1.0

        if (out != null) {
            try {
                out.flush();
                return;
            } catch (IOException e) {
                // Ignored, fall through to setError
            }
        }
        setError();
    
public java.io.PrintStreamformat(java.lang.String format, java.lang.Object args)
Writes a string formatted by an intermediate {@code Formatter} to the target stream using the specified format string and arguments. For the locale, the default value of the current virtual machine instance is used.

param
format the format string used for {@link java.util.Formatter#format}.
param
args the list of arguments passed to the formatter. If there are more arguments than required by the {@code format} string, then the additional arguments are ignored.
return
this stream.
throws
IllegalFormatException if the format string is illegal or incompatible with the arguments, if there are not enough arguments or if any other error regarding the format string or arguments is detected.
throws
NullPointerException if {@code format} is {@code null}.
since
Android 1.0

        return format(Locale.getDefault(), format, args);
    
public java.io.PrintStreamformat(java.util.Locale l, java.lang.String format, java.lang.Object args)
Writes a string formatted by an intermediate {@link Formatter} to this stream using the specified locale, format string and arguments.

param
l the locale used in the method. No localization will be applied if {@code l} is {@code null}.
param
format the format string used for {@link java.util.Formatter#format}.
param
args the list of arguments passed to the formatter. If there are more arguments than required by the {@code format} string, then the additional arguments are ignored.
return
this stream.
throws
IllegalFormatException if the format string is illegal or incompatible with the arguments, if there are not enough arguments or if any other error regarding the format string or arguments is detected.
throws
NullPointerException if {@code format} is {@code null}.
since
Android 1.0

        if (format == null) {
            throw new NullPointerException(Msg.getString("K0351")); //$NON-NLS-1$
        }
        new Formatter(this, l).format(format, args);
        return this;
    
private voidnewline()
Put the line separator String onto the print stream.

        print(lineSeparator);
    
public voidprint(char[] charArray)
Prints the string representation of the specified character array to the target stream.

param
charArray the character array to print to the target stream.
see
#print(String)
since
Android 1.0

        print(new String(charArray, 0, charArray.length));
    
public voidprint(char ch)
Prints the string representation of the specified character to the target stream.

param
ch the character to print to the target stream.
see
#print(String)
since
Android 1.0

        print(String.valueOf(ch));
    
public voidprint(double dnum)
Prints the string representation of the specified double to the target stream.

param
dnum the double value to print to the target stream.
see
#print(String)
since
Android 1.0

        print(String.valueOf(dnum));
    
public voidprint(float fnum)
Prints the string representation of the specified float to the target stream.

param
fnum the float value to print to the target stream.
see
#print(String)
since
Android 1.0

        print(String.valueOf(fnum));
    
public voidprint(int inum)
Prints the string representation of the specified integer to the target stream.

param
inum the integer value to print to the target stream.
see
#print(String)
since
Android 1.0

        print(String.valueOf(inum));
    
public voidprint(long lnum)
Prints the string representation of the specified long to the target stream.

param
lnum the long value to print to the target stream.
see
#print(String)
since
Android 1.0

        print(String.valueOf(lnum));
    
public voidprint(java.lang.Object obj)
Prints the string representation of the specified object to the target stream.

param
obj the object to print to the target stream.
see
#print(String)
since
Android 1.0

        print(String.valueOf(obj));
    
public synchronized voidprint(java.lang.String str)
Prints a string to the target stream. The string is converted to an array of bytes using the encoding chosen during the construction of this stream. The bytes are then written to the target stream with {@code write(int)}.

If an I/O error occurs, this stream's error state is set to {@code true}.

param
str the string to print to the target stream.
see
#write(int)
since
Android 1.0

        if (out == null) {
            setError();
            return;
        }
        if (str == null) {
            print("null"); //$NON-NLS-1$
            return;
        }

        try {
            if (encoding == null) {
                write(str.getBytes());
            } else {
                write(str.getBytes(encoding));
            }
        } catch (IOException e) {
            setError();
        }
    
public voidprint(boolean bool)
Prints the string representation of the specified boolean to the target stream.

param
bool the boolean value to print the target stream.
see
#print(String)
since
Android 1.0

        print(String.valueOf(bool));
    
public java.io.PrintStreamprintf(java.lang.String format, java.lang.Object args)
Prints a formatted string. The behavior of this method is the same as this stream's {@code #format(String, Object...)} method. For the locale, the default value of the current virtual machine instance is used.

param
format the format string used for {@link java.util.Formatter#format}.
param
args the list of arguments passed to the formatter. If there are more arguments than required by the {@code format} string, then the additional arguments are ignored.
return
this stream.
throws
IllegalFormatException if the format string is illegal or incompatible with the arguments, if there are not enough arguments or if any other error regarding the format string or arguments is detected.
throws
NullPointerException if {@code format} is {@code null}.
since
Android 1.0

        return format(format, args);
    
public java.io.PrintStreamprintf(java.util.Locale l, java.lang.String format, java.lang.Object args)
Prints a formatted string. The behavior of this method is the same as this stream's {@code #format(Locale, String, Object...)} method.

param
l the locale used in the method. No localization will be applied if {@code l} is {@code null}.
param
format the format string used for {@link java.util.Formatter#format}.
param
args the list of arguments passed to the formatter. If there are more arguments than required by the {@code format} string, then the additional arguments are ignored.
return
this stream.
throws
IllegalFormatException if the format string is illegal or incompatible with the arguments, if there are not enough arguments or if any other error regarding the format string or arguments is detected.
throws
NullPointerException if {@code format} is {@code null}.
since
Android 1.0

        return format(l, format, args);
    
public voidprintln()
Prints the string representation of the system property {@code "line.separator"} to the target stream.

since
Android 1.0

        newline();
    
public voidprintln(char[] charArray)
Prints the string representation of the specified character array followed by the system property {@code "line.separator"} to the target stream.

param
charArray the character array to print to the target stream.
see
#print(String)
since
Android 1.0

        println(new String(charArray, 0, charArray.length));
    
public voidprintln(char ch)
Prints the string representation of the specified character followed by the system property {@code "line.separator"} to the target stream.

param
ch the character to print to the target stream.
see
#print(String)
since
Android 1.0

        println(String.valueOf(ch));
    
public voidprintln(double dnum)
Prints the string representation of the specified double followed by the system property {@code "line.separator"} to the target stream.

param
dnum the double value to print to the target stream.
see
#print(String)
since
Android 1.0

        println(String.valueOf(dnum));
    
public voidprintln(float fnum)
Prints the string representation of the specified float followed by the system property {@code "line.separator"} to the target stream.

param
fnum the float value to print to the target stream.
see
#print(String)
since
Android 1.0

        println(String.valueOf(fnum));
    
public voidprintln(int inum)
Prints the string representation of the specified integer followed by the system property {@code "line.separator"} to the target stream.

param
inum the integer value to print to the target stream.
see
#print(String)
since
Android 1.0

        println(String.valueOf(inum));
    
public voidprintln(long lnum)
Prints the string representation of the specified long followed by the system property {@code "line.separator"} to the target stream.

param
lnum the long value to print to the target stream.
see
#print(String)
since
Android 1.0

        println(String.valueOf(lnum));
    
public voidprintln(java.lang.Object obj)
Prints the string representation of the specified object followed by the system property {@code "line.separator"} to the target stream.

param
obj the object to print to the target stream.
see
#print(String)
since
Android 1.0

        println(String.valueOf(obj));
    
public synchronized voidprintln(java.lang.String str)
Prints a string followed by the system property {@code "line.separator"} to the target stream. The string is converted to an array of bytes using the encoding chosen during the construction of this stream. The bytes are then written to the target stream with {@code write(int)}.

If an I/O error occurs, this stream's error state is set to {@code true}.

param
str the string to print to the target stream.
see
#write(int)
since
Android 1.0

        print(str);
        newline();
    
public voidprintln(boolean bool)
Prints the string representation of the specified boolean followed by the system property {@code "line.separator"} to the target stream.

param
bool the boolean value to print to the target stream.
see
#print(String)
since
Android 1.0

        println(String.valueOf(bool));
    
protected voidsetError()
Sets the error flag of this print stream to {@code true}.

since
Android 1.0

        ioError = true;
    
public voidwrite(byte[] buffer, int offset, int count)
Writes {@code count} bytes from {@code buffer} starting at {@code offset} to the target stream. If autoflush is set, this stream gets flushed after writing the buffer.

This stream's error flag is set to {@code true} if this stream is closed or an I/O error occurs.

param
buffer the buffer to be written.
param
offset the index of the first byte in {@code buffer} to write.
param
count the number of bytes in {@code buffer} to write.
throws
IndexOutOfBoundsException if {@code offset < 0} or {@code count < 0}, or if {@code offset + count} is bigger than the length of {@code buffer}.
see
#flush()
since
Android 1.0

        // BEGIN android-changed
        if (buffer == null) {
            throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
        }
        // avoid int overflow
        // Exception priorities (in case of multiple errors) differ from
        // RI, but are spec-compliant.
        // removed redundant check, used (offset | count) < 0
        // instead of (offset < 0) || (count < 0) to safe one operation
        if ((offset | count) < 0 || count > buffer.length - offset) {
            throw new ArrayIndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
        }
        // END android-changed
        synchronized (this) {
            if (out == null) {
                setError();
                return;
            }
            try {
                out.write(buffer, offset, count);
                if (autoflush) {
                    flush();
                }
            } catch (IOException e) {
                setError();
            }
        }
    
public synchronized voidwrite(int oneByte)
Writes one byte to the target stream. Only the least significant byte of the integer {@code oneByte} is written. This stream is flushed if {@code oneByte} is equal to the character {@code '\n'} and this stream is set to autoflush.

This stream's error flag is set to {@code true} if it is closed or an I/O error occurs.

param
oneByte the byte to be written
since
Android 1.0

        if (out == null) {
            setError();
            return;
        }
        try {
            out.write(oneByte);
            if (autoflush && (oneByte & 0xFF) == '\n") {
                flush();
            }
        } catch (IOException e) {
            setError();
        }