Most visited

Recently visited

Added in API level 1

PrintStream

public class PrintStream
extends FilterOutputStream implements Appendable, Closeable

java.lang.Object
   ↳ java.io.OutputStream
     ↳ java.io.FilterOutputStream
       ↳ java.io.PrintStream


PrintStream为另一个输出流增加了功能,即可方便地打印各种数据值的表示。 还提供了两个其他功能。 与其他输出流不同, PrintStream从不抛出IOException ; 相反,例外情况仅仅设置了一个可以通过checkError方法测试的内部标志。 可选地,可以创建PrintStream以便自动刷新; 这意味着在写入字节数组后, flush方法被自动调用, println方法之一被调用,或者写入换行符或字节( '\n' )。

所有由PrintStream打印的PrintStream都使用平台的默认字符编码转换为字节。 PrintWriter类应该用于需要编写字符而不是字节的情况。

Summary

Inherited fields

From class java.io.FilterOutputStream

Public constructors

PrintStream(OutputStream out)

创建一个新的打印流。

PrintStream(OutputStream out, boolean autoFlush)

创建一个新的打印流。

PrintStream(OutputStream out, boolean autoFlush, String encoding)

创建一个新的打印流。

PrintStream(String fileName)

使用指定的文件名创建新的打印流,不会自动行刷新。

PrintStream(String fileName, String csn)

使用指定的文件名和字符集创建新的打印流,不会自动清除行。

PrintStream(File file)

使用指定的文件创建新的打印流,不会自动行刷新。

PrintStream(File file, String csn)

使用指定的文件和字符集创建新的打印流,而不会自动行刷新。

Public methods

PrintStream append(char c)

将指定的字符附加到此输出流。

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

将指定字符序列的子序列附加到此输出流。

PrintStream append(CharSequence csq)

将指定的字符序列追加到此输出流。

boolean checkError()

刷新流并检查其错误状态。

void close()

关闭流。

void flush()

刷新流。

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

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

PrintStream 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)

打印一组字符。

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

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

PrintStream 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)

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

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

len字节从指定的字节数组开始,偏移量为 off写入此流。

void write(int b)

将指定的字节写入此流。

Protected methods

void clearError()

清除此流的内部错误状态。

void setError()

将流的错误状态设置为 true

Inherited methods

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

Public constructors

PrintStream

Added in API level 1
PrintStream (OutputStream out)

创建一个新的打印流。 此流不会自动刷新。

Parameters
out OutputStream: The output stream to which values and objects will be printed

也可以看看:

PrintStream

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

创建一个新的打印流。

Parameters
out OutputStream: The output stream to which values and objects will be printed
autoFlush boolean: 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

也可以看看:

PrintStream

Added in API level 1
PrintStream (OutputStream out, 
                boolean autoFlush, 
                String encoding)

创建一个新的打印流。

Parameters
out OutputStream: The output stream to which values and objects will be printed
autoFlush boolean: 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 String: The name of a supported character encoding
Throws
UnsupportedEncodingException If the named encoding is not supported

PrintStream

Added in API level 1
PrintStream (String fileName)

使用指定的文件名创建新的打印流,不会自动行刷新。 这个便捷的构造函数创建了必要的中间值OutputStreamWriter ,它将使用Java虚拟机的此实例的default charset对字符进行编码。

Parameters
fileName String: 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 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(fileName) denies write access to the file

PrintStream

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

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

Parameters
fileName String: 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 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(fileName) denies write access to the file
UnsupportedEncodingException If the named charset is not supported

PrintStream

Added in API level 1
PrintStream (File file)

使用指定的文件创建新的打印流,不会自动行刷新。 这个方便的构造函数创建必要的中间值OutputStreamWriter ,该中间OutputStreamWriter将使用default charset为Java虚拟机的此实例编码字符。

Parameters
file 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 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

PrintStream

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

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

Parameters
file 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 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 presentand 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
PrintStream append (char c)

将指定的字符附加到此输出流。

这种形式为 out.append(c)的方法的调用的行为方式与调用完全相同

     out.print(c) 

Parameters
c char: The 16-bit character to append
Returns
PrintStream This output stream

append

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

将指定字符序列的子序列附加到此输出流。

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

     out.print(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
PrintStream This output stream
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
PrintStream append (CharSequence csq)

将指定的字符序列追加到此输出流。

这种形式为 out.append(csq)的方法的调用的行为方式与调用完全相同

     out.print(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 output stream.
Returns
PrintStream This output stream

checkError

Added in API level 1
boolean checkError ()

刷新流并检查其错误状态。 内部错误状态设定为true当底层输出流抛出IOException比其他InterruptedIOException ,并且当setError被调用的方法。 如果对基础输出流的操作抛出InterruptedIOException ,则PrintStream通过执行以下PrintStream将异常转换回中断:

     Thread.currentThread().interrupt();
 
or the equivalent.

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

close

Added in API level 1
void close ()

关闭流。 这是通过刷新流然后关闭底层输出流来完成的。

也可以看看:

flush

Added in API level 1
void flush ()

刷新流。 这是通过将任何缓冲的输出字节写入基础输出流,然后清除该流来完成的。

也可以看看:

format

Added in API level 1
PrintStream 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
PrintStream This output stream
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
PrintStream 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
PrintStream This output stream
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
PrintStream 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
PrintStream This output stream
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
PrintStream 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
PrintStream This output stream
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 to be printed.

println

Added in API level 1
void println (boolean x)

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

Parameters
x boolean: The boolean to be printed

println

Added in API level 1
void println (long x)

长时间打印,然后终止该行。 此方法的行为如同调用print(long) ,然后调用println()

Parameters
x long: a The long 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 to be printed.

println

Added in API level 1
void println (String x)

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

Parameters
x String: The String to be printed.

println

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

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

Parameters
x char: an array of chars to print.

println

Added in API level 1
void println (float x)

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

Parameters
x float: The float to be printed.

println

Added in API level 1
void println (double x)

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

Parameters
x double: The double to be printed.

write

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

len字节从指定的字节数组开始,从偏移量off写入此流。 如果启用自动刷新功能, flush调用flush方法。

请注意,字节将按给定方式写入; 要编写将根据平台的默认字符编码进行翻译的字符,请使用print(char)println(char)方法。

Parameters
buf byte: A byte array
off int: Offset from which to start taking bytes
len int: Number of bytes to write

write

Added in API level 1
void write (int b)

将指定的字节写入此流。 如果该字节是换行符,并且启用了自动刷新, flush调用flush方法。

请注意,该字节被写为给定; 要编写将根据平台的默认字符编码进行翻译的字符,请使用print(char)println(char)方法。

Parameters
b int: The byte to be written

也可以看看:

Protected methods

clearError

Added in API level 9
void clearError ()

清除此流的内部错误状态。

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

setError

Added in API level 1
void setError ()

将流的错误状态设置为 true

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

Hooray!