Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
java.io.Writer
java.io.PrintWriter
public class PrintWriter
extends Writer
checkError()
method.
Additionally, this stream can be designated as "autoflush" when
created so that any writes are automatically flushed to the underlying
output sink whenever one of the println
methods is
called. (Note that this differs from the PrintStream
class which also auto-flushes when it encounters a newline character
in the chars written).
Constructor Summary | |
| |
| |
| |
| |
| |
| |
| |
|
Method Summary | |
PrintWriter |
|
PrintWriter |
|
PrintWriter |
|
boolean |
|
void |
|
void |
|
PrintWriter | |
PrintWriter | |
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void | |
void | |
void |
|
PrintWriter | |
PrintWriter | |
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void | |
void | |
void |
|
protected void |
|
void |
|
void |
|
void |
|
void | |
void |
Methods inherited from class java.io.Writer | |
append , append , append , close , flush , write , write , write , write , write |
Methods inherited from class java.lang.Object | |
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
public PrintWriter(File file) throws FileNotFoundException
This initializes a new PrintWriter object to write to the specified file. It creates a FileOutputStream object and wraps it in an OutputStreamWriter using the default encoding.
- Parameters:
file
- the file to write to
- Throws:
FileNotFoundException
- if the file cannot be written or created
- Since:
- 1.5
public PrintWriter(File file, String enc) throws FileNotFoundException, UnsupportedEncodingException
This initializes a new PrintWriter object to write to the specified file. It creates a FileOutputStream object and wraps it in an OutputStreamWriter using the specified encoding.
- Parameters:
file
- the file to write toenc
- the encoding to use
- Throws:
FileNotFoundException
- if the file cannot be written or createdUnsupportedEncodingException
- if the encoding is not supported
- Since:
- 1.5
public PrintWriter(OutputStream out)
This method initializes a newPrintWriter
object to write to the specifiedOutputStream
. Characters will be converted to chars using the system default encoding. Auto-flush functionality will not be enabled.
- Parameters:
out
- TheOutputStream
to write to
public PrintWriter(OutputStream out, boolean autoflush)
This method initializes a newPrintWriter
object to write to the specifiedOutputStream
. Characters will be converted to chars using the system default encoding. This form of the constructor allows auto-flush functionality to be enabled if desired
- Parameters:
out
- TheOutputStream
to write toautoflush
-true
to flush the stream after everyprintln
call,false
otherwise.
public PrintWriter(Writer wr)
This method intializes a newPrintWriter
object to write to the specified output sink. The form of the constructor does not enable auto-flush functionality.
- Parameters:
wr
- TheWriter
to write to.
public PrintWriter(Writer wr, boolean autoflush)
This method intializes a newPrintWriter
object to write to the specified output sink. This constructor also allows "auto-flush" functionality to be specified where the stream will be flushed after every line is terminated or newline character is written.
- Parameters:
wr
- TheWriter
to write to.autoflush
-true
to flush the stream after every line,false
otherwise
public PrintWriter(String file) throws FileNotFoundException
This initializes a new PrintWriter object to write to the specified file. It creates a FileOutputStream object and wraps it in an OutputStreamWriter using the default encoding.
- Parameters:
file
- name of the file to write to
- Throws:
FileNotFoundException
- if the file cannot be written or created
- Since:
- 1.5
public PrintWriter(String file, String enc) throws FileNotFoundException, UnsupportedEncodingException
This initializes a new PrintWriter object to write to the specified file. It creates a FileOutputStream object and wraps it in an OutputStreamWriter using the specified encoding.
- Parameters:
file
- name of the file to write toenc
- the encoding to use
- Throws:
FileNotFoundException
- if the file cannot be written or createdUnsupportedEncodingException
- if the encoding is not supported
- Since:
- 1.5
public PrintWriter append(CharSequence cs)
- Specified by:
- append in interface Appendable
- Since:
- 1.5
public PrintWriter append(CharSequence cs, int start, int end)
- Specified by:
- append in interface Appendable
- Since:
- 1.5
public boolean checkError()
This method checks to see if an error has occurred on this stream. Note that once an error has occurred, this method will continue to reporttrue
forever for this stream. Before checking for an error condition, this method flushes the stream.
- Returns:
true
if an error has occurred,false
otherwise
public void close()
This method closes this stream and all underlying streams.
- Specified by:
- close in interface Closeable
- close in interface AutoCloseable
public void flush()
This method flushes any buffered chars to the underlying stream and then flushes that stream as well.
public void print(boolean bool)
This methods prints a boolean value to the stream.true
values are printed as "true" andfalse
values are printed as "false".
- Parameters:
bool
- Theboolean
value to print
public void print(char ch)
This method prints a char to the stream. The actual value printed is determined by the character encoding in use.
- Parameters:
ch
- Thechar
value to be printed
public void print(char[] charArray)
This method prints an array of characters to the stream. The actual value printed depends on the system default encoding.
- Parameters:
charArray
- The array of characters to print.
public void print(double dnum)
This method prints a double to the stream. The value printed is determined using theString.valueOf()
method.
- Parameters:
dnum
- Thedouble
value to be printed
public void print(float fnum)
This method prints a float to the stream. The value printed is determined using theString.valueOf()
method.
- Parameters:
fnum
- Thefloat
value to be printed
public void print(int inum)
This method prints an integer to the stream. The value printed is determined using theString.valueOf()
method.
- Parameters:
inum
- Theint
value to be printed
public void print(Object obj)
This method prints anObject
to the stream. The actual value printed is determined by calling theString.valueOf()
method.
- Parameters:
obj
- TheObject
to print.
public void print(String str)
This method prints aString
to the stream. The actual value printed depends on the system default encoding.
- Parameters:
str
- TheString
to print.
public void print(long lnum)
This method prints a long to the stream. The value printed is determined using theString.valueOf()
method.
- Parameters:
lnum
- Thelong
value to be printed
public void println()
This method prints a line separator sequence to the stream. The value printed is determined by the system propertyline.separator and is not necessarily the Unix '\n' newline character.
public void println(boolean bool)
This methods prints a boolean value to the stream.true
values are printed as "true" andfalse
values are printed as "false". This method prints a line termination sequence after printing the value.
- Parameters:
bool
- Theboolean
value to print
public void println(char ch)
This method prints a char to the stream. The actual value printed is determined by the character encoding in use. This method prints a line termination sequence after printing the value.
- Parameters:
ch
- Thechar
value to be printed
public void println(char[] charArray)
This method prints an array of characters to the stream. The actual value printed depends on the system default encoding. This method prints a line termination sequence after printing the value.
- Parameters:
charArray
- The array of characters to print.
public void println(double dnum)
This method prints a double to the stream. The value printed is determined using theString.valueOf()
method. This method prints a line termination sequence after printing the value.
- Parameters:
dnum
- Thedouble
value to be printed
public void println(float fnum)
This method prints a float to the stream. The value printed is determined using theString.valueOf()
method. This method prints a line termination sequence after printing the value.
- Parameters:
fnum
- Thefloat
value to be printed
public void println(int inum)
This method prints an integer to the stream. The value printed is determined using theString.valueOf()
method. This method prints a line termination sequence after printing the value.
- Parameters:
inum
- Theint
value to be printed
public void println(Object obj)
This method prints anObject
to the stream. The actual value printed is determined by calling theString.valueOf()
method. This method prints a line termination sequence after printing the value.
- Parameters:
obj
- TheObject
to print.
public void println(String str)
This method prints aString
to the stream. The actual value printed depends on the system default encoding. This method prints a line termination sequence after printing the value.
- Parameters:
str
- TheString
to print.
public void println(long lnum)
This method prints a long to the stream. The value printed is determined using theString.valueOf()
method. This method prints a line termination sequence after printing the value.
- Parameters:
lnum
- Thelong
value to be printed
protected void setError()
This method can be called by subclasses to indicate that an error has occurred and should be reported bycheckError
.
public void write(char[] charArray)
This method write all the chars in the specified array to the output.
- Parameters:
charArray
- The array of characters to write
public void write(char[] charArray, int offset, int count)
This method writescount
chars from the specified array starting at indexoffset
into the array.
- Parameters:
charArray
- The array of chars to writeoffset
- The index into the array to start writing fromcount
- The number of chars to write
public void write(int ch)
This method writes a single char to the stream.
- Parameters:
ch
- The char to be written, passed as a int
public void write(String str)
This method writes the contents of the specifiedString
to the underlying stream.
- Parameters:
str
- TheString
to write
public void write(String str, int offset, int count)
This method writescount
chars from the specifiedString
to the output starting at character positionoffset
into theString
- Parameters:
str
- TheString
to write chars fromoffset
- The offset into theString
to start writing fromcount
- The number of chars to write.