java.io
Class PrintStream

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.io.OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream 
      extended by java.io.FilterOutputStream sample code for java.io.FilterOutputStream definition code for java.io.FilterOutputStream 
          extended by java.io.PrintStream
All Implemented Interfaces:
Closeable sample code for java.io.Closeable definition code for java.io.Closeable , Flushable sample code for java.io.Flushable definition code for java.io.Flushable , Appendable sample code for java.lang.Appendable definition code for java.lang.Appendable
Direct Known Subclasses:
LogStream sample code for java.rmi.server.LogStream definition code for java.rmi.server.LogStream

public class PrintStream
extends FilterOutputStream sample code for java.io.FilterOutputStream definition code for java.io.FilterOutputStream
implements Appendable sample code for java.lang.Appendable definition code for java.lang.Appendable , Closeable sample code for java.io.Closeable definition code for java.io.Closeable

A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Two other features are provided as well. Unlike other output streams, a PrintStream never throws an IOException; instead, exceptional situations merely set an internal flag that can be tested via the checkError method. Optionally, a PrintStream can be created so as to flush automatically; this means that the flush method is automatically invoked after a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written.

All characters printed by a PrintStream are converted into bytes using the platform's default character encoding. The PrintWriter sample code for java.io.PrintWriter definition code for java.io.PrintWriter class should be used in situations that require writing characters rather than bytes.

Since:
JDK1.0

Field Summary
 
Fields inherited from class java.io.FilterOutputStream sample code for java.io.FilterOutputStream definition code for java.io.FilterOutputStream
out sample code for java.io.FilterOutputStream.out definition code for java.io.FilterOutputStream.out
 
Constructor Summary
PrintStream sample code for java.io.PrintStream.PrintStream(java.io.File) definition code for java.io.PrintStream.PrintStream(java.io.File) (File sample code for java.io.File definition code for java.io.File  file)
          Creates a new print stream, without automatic line flushing, with the specified file.
PrintStream sample code for java.io.PrintStream.PrintStream(java.io.File, java.lang.String) definition code for java.io.PrintStream.PrintStream(java.io.File, java.lang.String) (File sample code for java.io.File definition code for java.io.File  file, String sample code for java.lang.String definition code for java.lang.String  csn)
          Creates a new print stream, without automatic line flushing, with the specified file and charset.
PrintStream sample code for java.io.PrintStream.PrintStream(java.io.OutputStream) definition code for java.io.PrintStream.PrintStream(java.io.OutputStream) (OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out)
          Create a new print stream.
PrintStream sample code for java.io.PrintStream.PrintStream(java.io.OutputStream, boolean) definition code for java.io.PrintStream.PrintStream(java.io.OutputStream, boolean) (OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out, boolean autoFlush)
          Create a new print stream.
PrintStream sample code for java.io.PrintStream.PrintStream(java.io.OutputStream, boolean, java.lang.String) definition code for java.io.PrintStream.PrintStream(java.io.OutputStream, boolean, java.lang.String) (OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out, boolean autoFlush, String sample code for java.lang.String definition code for java.lang.String  encoding)
          Create a new print stream.
PrintStream sample code for java.io.PrintStream.PrintStream(java.lang.String) definition code for java.io.PrintStream.PrintStream(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  fileName)
          Creates a new print stream, without automatic line flushing, with the specified file name.
PrintStream sample code for java.io.PrintStream.PrintStream(java.lang.String, java.lang.String) definition code for java.io.PrintStream.PrintStream(java.lang.String, java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  fileName, String sample code for java.lang.String definition code for java.lang.String  csn)
          Creates a new print stream, without automatic line flushing, with the specified file name and charset.
 
Method Summary
 PrintStream sample code for java.io.PrintStream definition code for java.io.PrintStream append sample code for java.io.PrintStream.append(char) definition code for java.io.PrintStream.append(char) (char c)
          Appends the specified character to this output stream.
 PrintStream sample code for java.io.PrintStream definition code for java.io.PrintStream append sample code for java.io.PrintStream.append(java.lang.CharSequence) definition code for java.io.PrintStream.append(java.lang.CharSequence) (CharSequence sample code for java.lang.CharSequence definition code for java.lang.CharSequence  csq)
          Appends the specified character sequence to this output stream.
 PrintStream sample code for java.io.PrintStream definition code for java.io.PrintStream append sample code for java.io.PrintStream.append(java.lang.CharSequence, int, int) definition code for java.io.PrintStream.append(java.lang.CharSequence, int, int) (CharSequence sample code for java.lang.CharSequence definition code for java.lang.CharSequence  csq, int start, int end)
          Appends a subsequence of the specified character sequence to this output stream.
 boolean checkError sample code for java.io.PrintStream.checkError() definition code for java.io.PrintStream.checkError() ()
          Flush the stream and check its error state.
 void close sample code for java.io.PrintStream.close() definition code for java.io.PrintStream.close() ()
          Close the stream.
 void flush sample code for java.io.PrintStream.flush() definition code for java.io.PrintStream.flush() ()
          Flush the stream.
 PrintStream sample code for java.io.PrintStream definition code for java.io.PrintStream format sample code for java.io.PrintStream.format(java.util.Locale, java.lang.String, java.lang.Object...) definition code for java.io.PrintStream.format(java.util.Locale, java.lang.String, java.lang.Object...) (Locale sample code for java.util.Locale definition code for java.util.Locale  l, String sample code for java.lang.String definition code for java.lang.String  format, Object sample code for java.lang.Object definition code for java.lang.Object ... args)
          Writes a formatted string to this output stream using the specified format string and arguments.
 PrintStream sample code for java.io.PrintStream definition code for java.io.PrintStream format sample code for java.io.PrintStream.format(java.lang.String, java.lang.Object...) definition code for java.io.PrintStream.format(java.lang.String, java.lang.Object...) (String sample code for java.lang.String definition code for java.lang.String  format, Object sample code for java.lang.Object definition code for java.lang.Object ... args)
          Writes a formatted string to this output stream using the specified format string and arguments.
 void print sample code for java.io.PrintStream.print(boolean) definition code for java.io.PrintStream.print(boolean) (boolean b)
          Print a boolean value.
 void print sample code for java.io.PrintStream.print(char) definition code for java.io.PrintStream.print(char) (char c)
          Print a character.
 void print sample code for java.io.PrintStream.print(char[]) definition code for java.io.PrintStream.print(char[]) (char[] s)
          Print an array of characters.
 void print sample code for java.io.PrintStream.print(double) definition code for java.io.PrintStream.print(double) (double d)
          Print a double-precision floating-point number.
 void print sample code for java.io.PrintStream.print(float) definition code for java.io.PrintStream.print(float) (float f)
          Print a floating-point number.
 void print sample code for java.io.PrintStream.print(int) definition code for java.io.PrintStream.print(int) (int i)
          Print an integer.
 void print sample code for java.io.PrintStream.print(long) definition code for java.io.PrintStream.print(long) (long l)
          Print a long integer.
 void print sample code for java.io.PrintStream.print(java.lang.Object) definition code for java.io.PrintStream.print(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  obj)
          Print an object.
 void print sample code for java.io.PrintStream.print(java.lang.String) definition code for java.io.PrintStream.print(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  s)
          Print a string.
 PrintStream sample code for java.io.PrintStream definition code for java.io.PrintStream printf sample code for java.io.PrintStream.printf(java.util.Locale, java.lang.String, java.lang.Object...) definition code for java.io.PrintStream.printf(java.util.Locale, java.lang.String, java.lang.Object...) (Locale sample code for java.util.Locale definition code for java.util.Locale  l, String sample code for java.lang.String definition code for java.lang.String  format, Object sample code for java.lang.Object definition code for java.lang.Object ... args)
          A convenience method to write a formatted string to this output stream using the specified format string and arguments.
 PrintStream sample code for java.io.PrintStream definition code for java.io.PrintStream printf sample code for java.io.PrintStream.printf(java.lang.String, java.lang.Object...) definition code for java.io.PrintStream.printf(java.lang.String, java.lang.Object...) (String sample code for java.lang.String definition code for java.lang.String  format, Object sample code for java.lang.Object definition code for java.lang.Object ... args)
          A convenience method to write a formatted string to this output stream using the specified format string and arguments.
 void println sample code for java.io.PrintStream.println() definition code for java.io.PrintStream.println() ()
          Terminate the current line by writing the line separator string.
 void println sample code for java.io.PrintStream.println(boolean) definition code for java.io.PrintStream.println(boolean) (boolean x)
          Print a boolean and then terminate the line.
 void println sample code for java.io.PrintStream.println(char) definition code for java.io.PrintStream.println(char) (char x)
          Print a character and then terminate the line.
 void println sample code for java.io.PrintStream.println(char[]) definition code for java.io.PrintStream.println(char[]) (char[] x)
          Print an array of characters and then terminate the line.
 void println sample code for java.io.PrintStream.println(double) definition code for java.io.PrintStream.println(double) (double x)
          Print a double and then terminate the line.
 void println sample code for java.io.PrintStream.println(float) definition code for java.io.PrintStream.println(float) (float x)
          Print a float and then terminate the line.
 void println sample code for java.io.PrintStream.println(int) definition code for java.io.PrintStream.println(int) (int x)
          Print an integer and then terminate the line.
 void println sample code for java.io.PrintStream.println(long) definition code for java.io.PrintStream.println(long) (long x)
          Print a long and then terminate the line.
 void println sample code for java.io.PrintStream.println(java.lang.Object) definition code for java.io.PrintStream.println(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  x)
          Print an Object and then terminate the line.
 void println sample code for java.io.PrintStream.println(java.lang.String) definition code for java.io.PrintStream.println(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  x)
          Print a String and then terminate the line.
protected  void setError sample code for java.io.PrintStream.setError() definition code for java.io.PrintStream.setError() ()
          Set the error state of the stream to true.
 void write sample code for java.io.PrintStream.write(byte[], int, int) definition code for java.io.PrintStream.write(byte[], int, int) (byte[] buf, int off, int len)
          Write len bytes from the specified byte array starting at offset off to this stream.
 void write sample code for java.io.PrintStream.write(int) definition code for java.io.PrintStream.write(int) (int b)
          Write the specified byte to this stream.
 
Methods inherited from class java.io.FilterOutputStream sample code for java.io.FilterOutputStream definition code for java.io.FilterOutputStream
write sample code for java.io.FilterOutputStream.write(byte[]) definition code for java.io.FilterOutputStream.write(byte[])
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

PrintStream sample code for java.io.PrintStream(java.io.OutputStream) definition code for java.io.PrintStream(java.io.OutputStream)

public PrintStream(OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out)
Create a new print stream. This stream will not flush automatically.

Parameters:
out - The output stream to which values and objects will be printed
See Also:
PrintWriter.PrintWriter(java.io.OutputStream) sample code for java.io.PrintWriter.PrintWriter(java.io.OutputStream) definition code for java.io.PrintWriter.PrintWriter(java.io.OutputStream)

PrintStream sample code for java.io.PrintStream(java.io.OutputStream, boolean) definition code for java.io.PrintStream(java.io.OutputStream, boolean)

public PrintStream(OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out,
                   boolean autoFlush)
Create a new print stream.

Parameters:
out - The output stream to which values and objects will be printed
autoFlush - A boolean; if true, the output buffer will be flushed whenever a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written
See Also:
PrintWriter.PrintWriter(java.io.OutputStream, boolean) sample code for java.io.PrintWriter.PrintWriter(java.io.OutputStream, boolean) definition code for java.io.PrintWriter.PrintWriter(java.io.OutputStream, boolean)

PrintStream sample code for java.io.PrintStream(java.io.OutputStream, boolean, java.lang.String) definition code for java.io.PrintStream(java.io.OutputStream, boolean, java.lang.String)

public PrintStream(OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out,
                   boolean autoFlush,
                   String sample code for java.lang.String definition code for java.lang.String  encoding)
            throws UnsupportedEncodingException sample code for java.io.UnsupportedEncodingException definition code for java.io.UnsupportedEncodingException 
Create a new print stream.

Parameters:
out - The output stream to which values and objects will be printed
autoFlush - A boolean; if true, the output buffer will be flushed whenever a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written
encoding - The name of a supported character encoding
Throws:
UnsupportedEncodingException sample code for java.io.UnsupportedEncodingException definition code for java.io.UnsupportedEncodingException - If the named encoding is not supported

PrintStream sample code for java.io.PrintStream(java.lang.String) definition code for java.io.PrintStream(java.lang.String)

public PrintStream(String sample code for java.lang.String definition code for java.lang.String  fileName)
            throws FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.FileNotFoundException 
Creates a new print stream, without automatic line flushing, with the specified file name. This convenience constructor creates the necessary intermediate OutputStreamWriter sample code for java.io.OutputStreamWriter definition code for java.io.OutputStreamWriter , which will encode characters using the default charset sample code for java.nio.charset.Charset.defaultCharset definition code for java.nio.charset.Charset.defaultCharset for this instance of the Java virtual machine.

Parameters:
fileName - The name of the file to use as the destination of this print stream. If the file exists, then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
Throws:
FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - If a security manager is present and checkWrite(fileName) sample code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor) definition code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor) denies write access to the file
Since:
1.5

PrintStream sample code for java.io.PrintStream(java.lang.String, java.lang.String) definition code for java.io.PrintStream(java.lang.String, java.lang.String)

public PrintStream(String sample code for java.lang.String definition code for java.lang.String  fileName,
                   String sample code for java.lang.String definition code for java.lang.String  csn)
            throws FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.FileNotFoundException ,
                   UnsupportedEncodingException sample code for java.io.UnsupportedEncodingException definition code for java.io.UnsupportedEncodingException 
Creates a new print stream, without automatic line flushing, with the specified file name and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter sample code for java.io.OutputStreamWriter definition code for java.io.OutputStreamWriter , which will encode characters using the provided charset.

Parameters:
fileName - The name of the file to use as the destination of this print stream. If the file exists, then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
csn - The name of a supported charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset
Throws:
FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - If a security manager is present and checkWrite(fileName) sample code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor) definition code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor) denies write access to the file
UnsupportedEncodingException sample code for java.io.UnsupportedEncodingException definition code for java.io.UnsupportedEncodingException - If the named charset is not supported
Since:
1.5

PrintStream sample code for java.io.PrintStream(java.io.File) definition code for java.io.PrintStream(java.io.File)

public PrintStream(File sample code for java.io.File definition code for java.io.File  file)
            throws FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.FileNotFoundException 
Creates a new print stream, without automatic line flushing, with the specified file. This convenience constructor creates the necessary intermediate OutputStreamWriter sample code for java.io.OutputStreamWriter definition code for java.io.OutputStreamWriter , which will encode characters using the default charset sample code for java.nio.charset.Charset.defaultCharset definition code for java.nio.charset.Charset.defaultCharset for this instance of the Java virtual machine.

Parameters:
file - The file to use as the destination of this print stream. If the file exists, then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
Throws:
FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - If a security manager is present and checkWrite(file.getPath()) sample code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor) definition code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor) denies write access to the file
Since:
1.5

PrintStream sample code for java.io.PrintStream(java.io.File, java.lang.String) definition code for java.io.PrintStream(java.io.File, java.lang.String)

public PrintStream(File sample code for java.io.File definition code for java.io.File  file,
                   String sample code for java.lang.String definition code for java.lang.String  csn)
            throws FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.FileNotFoundException ,
                   UnsupportedEncodingException sample code for java.io.UnsupportedEncodingException definition code for java.io.UnsupportedEncodingException 
Creates a new print stream, without automatic line flushing, with the specified file and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter sample code for java.io.OutputStreamWriter definition code for java.io.OutputStreamWriter , which will encode characters using the provided charset.

Parameters:
file - The file to use as the destination of this print stream. If the file exists, then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
csn - The name of a supported charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset
Throws:
FileNotFoundException sample code for java.io.FileNotFoundException definition code for java.io.FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - If a security manager is presentand checkWrite(file.getPath()) sample code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor) definition code for java.lang.SecurityManager.checkWrite(java.io.FileDescriptor) denies write access to the file
UnsupportedEncodingException sample code for java.io.UnsupportedEncodingException definition code for java.io.UnsupportedEncodingException - If the named charset is not supported
Since:
1.5
Method Detail

flush sample code for java.io.PrintStream.flush() definition code for java.io.PrintStream.flush()

public void flush()
Flush the stream. This is done by writing any buffered output bytes to the underlying output stream and then flushing that stream.

Specified by:
flush sample code for java.io.Flushable.flush() definition code for java.io.Flushable.flush() in interface Flushable sample code for java.io.Flushable definition code for java.io.Flushable
Overrides:
flush sample code for java.io.FilterOutputStream.flush() definition code for java.io.FilterOutputStream.flush() in class FilterOutputStream sample code for java.io.FilterOutputStream definition code for java.io.FilterOutputStream
See Also:
OutputStream.flush() sample code for java.io.OutputStream.flush() definition code for java.io.OutputStream.flush()

close sample code for java.io.PrintStream.close() definition code for java.io.PrintStream.close()

public void close()
Close the stream. This is done by flushing the stream and then closing the underlying output stream.

Specified by:
close sample code for java.io.Closeable.close() definition code for java.io.Closeable.close() in interface Closeable sample code for java.io.Closeable definition code for java.io.Closeable
Overrides:
close sample code for java.io.FilterOutputStream.close() definition code for java.io.FilterOutputStream.close() in class FilterOutputStream sample code for java.io.FilterOutputStream definition code for java.io.FilterOutputStream
See Also:
OutputStream.close() sample code for java.io.OutputStream.close() definition code for java.io.OutputStream.close()

checkError sample code for java.io.PrintStream.checkError() definition code for java.io.PrintStream.checkError()

public boolean checkError()
Flush the stream and check its error state. The internal error state is set to true when the underlying output stream throws an IOException other than InterruptedIOException, and when the setError method is invoked. If an operation on the underlying output stream throws an InterruptedIOException, then the PrintStream converts the exception back into an interrupt by doing:
     Thread.currentThread().interrupt();
 
or the equivalent.

Returns:
True if and only if this stream has encountered an IOException other than InterruptedIOException, or the setError method has been invoked

setError sample code for java.io.PrintStream.setError() definition code for java.io.PrintStream.setError()

protected void setError()
Set the error state of the stream to true.

Since:
JDK1.1

write sample code for java.io.PrintStream.write(int) definition code for java.io.PrintStream.write(int)

public void write(int b)
Write the specified byte to this stream. If the byte is a newline and automatic flushing is enabled then the flush method will be invoked.

Note that the byte is written as given; to write a character that will be translated according to the platform's default character encoding, use the print(char) or println(char) methods.

Overrides:
write sample code for java.io.FilterOutputStream.write(int) definition code for java.io.FilterOutputStream.write(int) in class FilterOutputStream sample code for java.io.FilterOutputStream definition code for java.io.FilterOutputStream
Parameters:
b - The byte to be written
See Also:
print(char) sample code for java.io.PrintStream.print(char) definition code for java.io.PrintStream.print(char) , println(char) sample code for java.io.PrintStream.println(char) definition code for java.io.PrintStream.println(char)

write