Java Reference
In-Depth Information
protocol requires. More often than not, the required terminator is a carriage return/
linefeed pair.
PrintWriter
The
PrintWriter
class is a replacement for Java 1.0's
PrintStream
class that properly
handles multibyte character sets and international text. Sun originally planned to dep‐
recate
PrintStream
in favor of
PrintWriter
but backed off when it realized this step
would invalidate too much existing code, especially code that depended on
Sys
tem.out
. Nonetheless, new code should use
PrintWriter
instead of
PrintStream
.
Aside from the constructors, the
PrintWriter
class has an almost identical collection
of methods to
PrintStream
. These include:
public
PrintWriter
(
Writer
out
)
public
PrintWriter
(
Writer
out
,
boolean
autoFlush
)
public
PrintWriter
(
OutputStream
out
)
public
PrintWriter
(
OutputStream
out
,
boolean
autoFlush
)
public
void
flush
()
public
void
close
()
public
boolean
checkError
()
public
void
write
(
int
c
)
public
void
write
(
char
[]
text
,
int
offset
,
int
length
)
public
void
write
(
char
[]
text
)
public
void
write
(
String
s
,
int
offset
,
int
length
)
public
void
write
(
String
s
)
public
void
print
(
boolean
b
)
public
void
print
(
char
c
)
public
void
print
(
int
i
)
public
void
print
(
long
l
)
public
void
print
(
float
f
)
public
void
print
(
double
d
)
public
void
print
(
char
[]
text
)
public
void
print
(
String
s
)
public
void
print
(
Object
o
)
public
void
println
()
public
void
println
(
boolean
b
)
public
void
println
(
char
c
)
public
void
println
(
int
i
)
public
void
println
(
long
l
)
public
void
println
(
float
f
)
public
void
println
(
double
d
)
public
void
println
(
char
[]
text
)
public
void
println
(
String
s
)
public
void
println
(
Object
o
)
Most of these methods behave the same for
PrintWriter
as they do for
PrintStream
.
The exceptions are the four
write()
methods, which write characters rather than bytes;
also, if the underlying writer properly handles character set conversion, so do all the
methods of the
PrintWriter
. This is an improvement over the noninternationalizable