Most visited

Recently visited

Added in API level 1

PrintWriter

public class PrintWriter
extends Writer

java.lang.Object
   ↳ java.io.Writer
     ↳ java.io.PrintWriter


将对象的格式化表示形式打印到文本输出流。 这个类实现所有中找到的print种方法PrintStream 它不包含写入原始字节的方法,程序应使用未编码的字节流。

PrintStream类不同,如果启用了自动刷新功能,则只有在调用println,printfformat方法之一时才会完成此操作 ,而不是每当恰好输出换行符时。 这些方法使用平台自己的行分隔符而不是换行符。

该类中的方法不会抛出I / O异常,尽管它的一些构造函数可能会这样。 客户可以通过调用checkError()来查询是否发生了错误。

Summary

Fields

protected Writer out

这个 PrintWriter的底层字符输出流。

Inherited fields

From class java.io.Writer

Public constructors

PrintWriter(Writer out)

创建一个新的PrintWriter,不会自动行刷新。

PrintWriter(Writer out, boolean autoFlush)

创建一个新的PrintWriter。

PrintWriter(OutputStream out)

从现有的OutputStream中创建一个新的PrintWriter,不会自动清除行。

PrintWriter(OutputStream out, boolean autoFlush)

从现有的OutputStream创建一个新的PrintWriter。

PrintWriter(String fileName)

用指定的文件名创建一个新的PrintWriter,不用自动换行。

PrintWriter(String fileName, String csn)

用指定的文件名和字符集创建一个新的PrintWriter,不用自动换行。

PrintWriter(File file)

用指定的文件创建一个新的PrintWriter,不用自动换行。

PrintWriter(File file, String csn)

用指定的文件和字符集创建一个新的PrintWriter,不用自动换行。

Public methods

PrintWriter append(char c)

将指定的字符附加到此作者。

PrintWriter append(CharSequence csq, int start, int end)

将指定字符序列的子序列附加到此作者。

PrintWriter append(CharSequence csq)

将指定的字符序列追加到此作者。

boolean checkError()

刷新流如果没有关闭并检查其错误状态。

void close()

关闭流并释放与其关联的所有系统资源。

void flush()

刷新流。

PrintWriter format(Locale l, String format, Object... args)

使用指定的格式字符串和参数将格式化的字符串写入此写入器。

PrintWriter format(String format, Object... args)

使用指定的格式字符串和参数将格式化的字符串写入此写入器。

void print(int i)

打印一个整数。

void print(double d)

打印一个双精度浮点数。

void print(boolean b)

打印一个布尔值。

void print(char c)

打印一个角色。

void print(long l)

打印一个长整数。

void print(float f)

打印一个浮点数。

void print(String s)

打印一个字符串。

void print(Object obj)

打印一个对象。

void print(char[] s)

打印一组字符。

PrintWriter printf(String format, Object... args)

使用指定的格式字符串和参数将格式化的字符串写入此写入器的便捷方法。

PrintWriter printf(Locale l, String format, Object... args)

使用指定的格式字符串和参数将格式化的字符串写入此写入器的便捷方法。

void println(Object x)

打印一个对象,然后终止该行。

void println(char x)

打印一个字符,然后终止该行。

void println(boolean x)

打印一个布尔值,然后终止该行。

void println(long x)

打印一个长整数,然后终止该行。

void println()

通过写入行分隔符字符串来终止当前行。

void println(int x)

打印一个整数,然后终止该行。

void println(String x)

打印一个字符串,然后终止该行。

void println(char[] x)

打印一组字符,然后终止该行。

void println(float x)

打印一个浮点数,然后终止该行。

void println(double x)

打印一个双精度浮点数,然后终止该行。

void write(int c)

写一个字符。

void write(char[] buf, int off, int len)

写入一组字符的部分。

void write(String s)

写入一个字符串。

void write(String s, int off, int len)

写入一部分字符串。

void write(char[] buf)

写入一个字符数组。

Protected methods

void clearError()

清除此流的错误状态。

void setError()

表示发生错误。

Inherited methods

From class java.io.Writer
From class java.lang.Object
From interface java.lang.Appendable
From interface java.io.Closeable
From interface java.io.Flushable
From interface java.lang.AutoCloseable

Fields

out

Added in API level 1
Writer out

这个 PrintWriter的底层字符输出流。

Public constructors

PrintWriter

Added in API level 1
PrintWriter (Writer out)

创建一个新的PrintWriter,不会自动行刷新。

Parameters
out Writer: A character-output stream

PrintWriter

Added in API level 1
PrintWriter (Writer out, 
                boolean autoFlush)

创建一个新的PrintWriter。

Parameters
out Writer: A character-output stream
autoFlush boolean: A boolean; if true, the println, printf, or format methods will flush the output buffer

PrintWriter

Added in API level 1
PrintWriter (OutputStream out)

从现有的OutputStream中创建一个新的PrintWriter,不会自动清除行。 这个便捷的构造函数创建必要的中间OutputStreamWriter,它将使用默认字符编码将字符转换为字节。

Parameters
out OutputStream: An output stream

也可以看看:

PrintWriter

Added in API level 1
PrintWriter (OutputStream out, 
                boolean autoFlush)

从现有的OutputStream创建一个新的PrintWriter。 这个便捷的构造函数创建必要的中间OutputStreamWriter,它将使用默认字符编码将字符转换为字节。

Parameters
out OutputStream: An output stream
autoFlush boolean: A boolean; if true, the println, printf, or format methods will flush the output buffer

也可以看看:

PrintWriter

Added in API level 1
PrintWriter (String fileName)

用指定的文件名创建一个新的PrintWriter,不用自动换行。 这个便利的构造函数创建了必要的中间值OutputStreamWriter ,它将使用default charset为Java虚拟机的此实例编码字符。

Parameters
fileName String: The name of the file to use as the destination of this writer. 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 If the given string 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 If a security manager is present and checkWrite(fileName) denies write access to the file

PrintWriter

Added in API level 1
PrintWriter (String fileName, 
                String csn)

用指定的文件名和字符集创建一个新的PrintWriter,不用自动换行。 这个便捷的构造函数创建了必要的中间值OutputStreamWriter ,它将使用提供的字符集对字符进行编码。

Parameters
fileName String: The name of the file to use as the destination of this writer. 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 String: The name of a supported charset
Throws
FileNotFoundException If the given string 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 If a security manager is present and checkWrite(fileName) denies write access to the file
UnsupportedEncodingException If the named charset is not supported

PrintWriter

Added in API level 1
PrintWriter (File file)

用指定的文件创建一个新的PrintWriter,不用自动换行。 这个便捷的构造函数创建了必要的中间值OutputStreamWriter ,它将使用default charset为Java虚拟机的此实例编码字符。

Parameters
file File: The file to use as the destination of this writer. 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 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 If a security manager is present and checkWrite(file.getPath()) denies write access to the file

PrintWriter

Added in API level 1
PrintWriter (File file, 
                String csn)

用指定的文件和字符集创建一个新的PrintWriter,不用自动换行。 这个便利的构造函数创建了必要的中间值OutputStreamWriter ,它将使用提供的字符集对字符进行编码。

Parameters
file File: The file to use as the destination of this writer. 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 String: The name of a supported charset
Throws
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 If a security manager is present and checkWrite(file.getPath()) denies write access to the file
UnsupportedEncodingException If the named charset is not supported

Public methods

append

Added in API level 1
PrintWriter append (char c)

将指定的字符附加到此作者。

表单 out.append(c)的此方法的调用的行为与调用完全相同

     out.write(c) 

Parameters
c char: The 16-bit character to append
Returns
PrintWriter This writer

append

Added in API level 1
PrintWriter append (CharSequence csq, 
                int start, 
                int end)

将指定字符序列的子序列附加到此作者。

形式的这种方法的调用时 out.append(csq, start, end) csq不是 null,行为以完全相同的方式调用

     out.write(csq.subSequence(start, end).toString()) 

Parameters
csq CharSequence: The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".
start int: The index of the first character in the subsequence
end int: The index of the character following the last character in the subsequence
Returns
PrintWriter This writer
Throws
IndexOutOfBoundsException If start or end are negative, start is greater than end, or end is greater than csq.length()

append

Added in API level 1
PrintWriter append (CharSequence csq)

将指定的字符序列追加到此作者。

调用表单 out.append(csq)的此方法的行为与调用完全相同

     out.write(csq.toString()) 

取决于toString字符序列csq本说明书中,整个序列可以不追加。 例如,调用字符缓冲区的toString方法将返回一个子序列,其内容取决于缓冲区的位置和限制。

Parameters
csq CharSequence: The character sequence to append. If csq is null, then the four characters "null" are appended to this writer.
Returns
PrintWriter This writer

checkError

Added in API level 1
boolean checkError ()

刷新流如果没有关闭并检查其错误状态。

Returns
boolean true if the print stream has encountered an error, either on the underlying output stream or during a format conversion.

close

Added in API level 1
void close ()

关闭流并释放与其关联的所有系统资源。 关闭以前关闭的流不起作用。

也可以看看:

flush

Added in API level 1
void flush ()

刷新流。

也可以看看:

format

Added in API level 1
PrintWriter format (Locale l, 
                String format, 
                Object... args)

使用指定的格式字符串和参数将格式化的字符串写入此写入器。 如果启用自动刷新,则调用此方法将刷新输出缓冲区。

Parameters
l Locale: The locale to apply during formatting. If l is null then no localization is applied.
format String: A format string as described in Format string syntax.
args Object: Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification. The behaviour on a null argument depends on the conversion.
Returns
PrintWriter This writer
Throws
If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
NullPointerException If the format is null

format

Added in API level 1
PrintWriter format (String format, 
                Object... args)

使用指定的格式字符串和参数将格式化的字符串写入此写入器。 如果启用自动刷新,则调用此方法将刷新输出缓冲区。

总是使用的语言环境是由 Locale.getDefault()返回的语言环境,而不管此对象以前是否有任何其他格式化方法的调用。

Parameters
format String: A format string as described in Format string syntax.
args Object: Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification. The behaviour on a null argument depends on the conversion.
Returns
PrintWriter This writer
Throws
If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the Formatter class specification.
NullPointerException If the format is null

print

Added in API level 1
void print (int i)

打印一个整数。 根据平台的默认字符编码将由valueOf(int)生成的字符串翻译为字节,并且这些字节以与write(int)方法完全相同的方式写入。

Parameters
i int: The int to be printed

也可以看看:

print

Added in API level 1
void print (double d)

打印一个双精度浮点数。 根据平台的默认字符编码将由valueOf(double)生成的字符串翻译为字节,并且这些字节以与write(int)方法完全相同的方式写入。

Parameters
d double: The double to be printed

也可以看看:

print

Added in API level 1
void print (boolean b)

打印一个布尔值。 根据平台的默认字符编码将由valueOf(boolean)生成的字符串翻译为字节,并且这些字节以与write(int)方法完全相同的方式写入。

Parameters
b boolean: The boolean to be printed

print

Added in API level 1
void print (char c)

打印一个角色。 根据平台的默认字符编码将字符翻译为一个或多个字节,并且这些字节的写入方式与write(int)方法完全相同。

Parameters
c char: The char to be printed

print

Added in API level 1
void print (long l)

打印一个长整数。 根据平台的默认字符编码将由valueOf(long)生成的字符串翻译为字节,并且这些字节以与write(int)方法完全相同的方式写入。

Parameters
l long: The long to be printed

也可以看看:

print

Added in API level 1
void print (float f)

打印一个浮点数。 根据平台的默认字符编码将由valueOf(float)生成的字符串翻译为字节,并且这些字节以与write(int)方法完全相同的方式写入。

Parameters
f float: The float to be printed

也可以看看:

print

Added in API level 1
void print (String s)

打印一个字符串。 如果参数是null则打印字符串"null" 否则,根据平台的默认字符编码将字符串的字符转换为字节,并且这些字节的写入方式与write(int)方法完全相同。

Parameters
s String: The String to be printed

print

Added in API level 1
void print (Object obj)

打印一个对象。 根据平台的默认字符编码将valueOf(Object)方法生成的字符串转换为字节,并且这些字节的写入方式与write(int)方法完全相同。

Parameters
obj Object: The Object to be printed

也可以看看:

print

Added in API level 1
void print (char[] s)

打印一组字符。 根据平台的默认字符编码将字符转换为字节,并且这些字节的写入方式与write(int)方法完全相同。

Parameters
s char: The array of chars to be printed
Throws
NullPointerException If s is null

printf

Added in API level 1
PrintWriter printf (String format, 
                Object... args)

使用指定的格式字符串和参数将格式化的字符串写入此写入器的便捷方法。 如果启用自动刷新,则调用此方法将刷新输出缓冲区。

表单 out.printf(format, args)的这种方法的调用的行为与调用完全相同

     out.format(format, args) 

Parameters
format String: A format string as described in Format string syntax.
args Object: Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification. The behaviour on a null argument depends on the conversion.
Returns
PrintWriter This writer
Throws
If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
NullPointerException If the format is null

printf

Added in API level 1
PrintWriter printf (Locale l, 
                String format, 
                Object... args)

使用指定的格式字符串和参数将格式化的字符串写入此写入器的便捷方法。 如果启用自动刷新,则调用此方法将刷新输出缓冲区。

表单 out.printf(l, format, args)的此方法的调用的行为与调用完全相同

     out.format(l, format, args) 

Parameters
l Locale: The locale to apply during formatting. If l is null then no localization is applied.
format String: A format string as described in Format string syntax.
args Object: Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification. The behaviour on a null argument depends on the conversion.
Returns
PrintWriter This writer
Throws
If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
NullPointerException If the format is null

println

Added in API level 1
void println (Object x)

打印一个对象,然后终止该行。 此方法首先调用String.valueOf(x)以获取打印对象的字符串值,然后表现得像调用print(String) ,然后调用println()

Parameters
x Object: The Object to be printed.

println

Added in API level 1
void println (char x)

打印一个字符,然后终止该行。 该方法表现得好像调用了print(char) ,然后调用了println()

Parameters
x char: the char value to be printed

println

Added in API level 1
void println (boolean x)

打印一个布尔值,然后终止该行。 该方法表现得好像调用了print(boolean) ,然后调用了println()

Parameters
x boolean: the boolean value to be printed

println

Added in API level 1
void println (long x)

打印一个长整数,然后终止该行。 此方法表现得好像调用print(long) ,然后调用println()

Parameters
x long: the long value to be printed

println

Added in API level 1
void println ()

通过写入行分隔符字符串来终止当前行。 行分隔符字符串由系统属性line.separator定义,并不一定是单个换行符( '\n' )。

println

Added in API level 1
void println (int x)

打印一个整数,然后终止该行。 该方法的行为如同调用print(int) ,然后调用println()

Parameters
x int: the int value to be printed

println

Added in API level 1
void println (String x)

打印一个字符串,然后终止该行。 此方法表现得好像调用了print(String) ,然后是println()

Parameters
x String: the String value to be printed

println

Added in API level 1
void println (char[] x)

打印一组字符,然后终止该行。 该方法表现得好像调用了print(char[]) ,然后调用了println()

Parameters
x char: the array of char values to be printed

println

Added in API level 1
void println (float x)

打印一个浮点数,然后终止该行。 此方法表现得好像调用了print(float) ,然后调用了println()

Parameters
x float: the float value to be printed

println

Added in API level 1
void println (double x)

打印一个双精度浮点数,然后终止该行。 此方法表现得好像调用了print(double) ,然后调用了println()

Parameters
x double: the double value to be printed

write

Added in API level 1
void write (int c)

写一个字符。

Parameters
c int: int specifying a character to be written.

write

Added in API level 1
void write (char[] buf, 
                int off, 
                int len)

写入一组字符的部分。

Parameters
buf char: Array of characters
off int: Offset from which to start writing characters
len int: Number of characters to write

write

Added in API level 1
void write (String s)

写入一个字符串。 此方法不能从Writer类继承,因为它必须抑制I / O异常。

Parameters
s String: String to be written

write

Added in API level 1
void write (String s, 
                int off, 
                int len)

写入一部分字符串。

Parameters
s String: A String
off int: Offset from which to start writing characters
len int: Number of characters to write

write

Added in API level 1
void write (char[] buf)

写入一个字符数组。 此方法不能从Writer类继承,因为它必须抑制I / O异常。

Parameters
buf char: Array of characters to be written

Protected methods

clearError

Added in API level 9
void clearError ()

清除此流的错误状态。

此方法将导致后续调用 checkError()返回 false,直到另一个写入操作失败并调用 setError()

setError

Added in API level 1
void setError ()

表示发生错误。

此方法将导致后续调用 checkError()返回 true,直到调用 clearError()

Hooray!