public class PrintStream extends FilterOutputStream implements Appendable, Closeable
PrintStream为另一个输出流添加了功能,即能够方便地打印各种数据值的表示。
还提供了另外两个功能。
与其他输出流不同, PrintStream从不抛出IOException ;
相反,异常情况只是设置一个可以通过checkError方法测试的内部标志。
可以选择一个PrintStream ,以便自动刷新;
这意味着flush字节数组写入方法后自动调用,所述一个println方法被调用时,或者一个新行字符或字节( '\n' )被写入。
由印刷的所有字符PrintStream被转换成使用平台的默认字符编码字节。 在需要编写字符而不是字节的情况下,应使用类。 PrintWriter
out| Constructor and Description |
|---|
PrintStream(File file)
使用指定的文件创建一个新的打印流,而不需要自动换行。
|
PrintStream(File file, String csn)
使用指定的文件和字符集创建新的打印流,而不需要自动换行。
|
PrintStream(OutputStream out)
创建一个新的打印流。
|
PrintStream(OutputStream out, boolean autoFlush)
创建一个新的打印流。
|
PrintStream(OutputStream out, boolean autoFlush, String encoding)
创建一个新的打印流。
|
PrintStream(String fileName)
使用指定的文件名创建新的打印流,无需自动换行。
|
PrintStream(String fileName, String csn)
创建一个新的打印流,不需要自动换行,具有指定的文件名和字符集。
|
| Modifier and Type | Method and Description |
|---|---|
PrintStream |
append(char c)
将指定的字符附加到此输出流。
|
PrintStream |
append(CharSequence csq)
将指定的字符序列附加到此输出流。
|
PrintStream |
append(CharSequence csq, int start, int end)
将指定字符序列的子序列附加到此输出流。
|
boolean |
checkError()
刷新流并检查其错误状态。
|
protected void |
clearError()
清除此流的内部错误状态。
|
void |
close()
关闭流。
|
void |
flush()
刷新流。
|
PrintStream |
format(Locale l, String format, Object... args)
使用指定的格式字符串和参数将格式化的字符串写入此输出流。
|
PrintStream |
format(String format, Object... args)
使用指定的格式字符串和参数将格式化的字符串写入此输出流。
|
void |
print(boolean b)
打印布尔值。
|
void |
print(char c)
打印一个字符
|
void |
print(char[] s)
打印字符数组。
|
void |
print(double d)
打印双精度浮点数。
|
void |
print(float f)
打印浮点数。
|
void |
print(int i)
打印一个整数。
|
void |
print(long l)
打印一个长整数。
|
void |
print(Object obj)
打印一个对象。
|
void |
print(String s)
打印字符串。
|
PrintStream |
printf(Locale l, String format, Object... args)
使用指定的格式字符串和参数将格式化的字符串写入此输出流的便利方法。
|
PrintStream |
printf(String format, Object... args)
使用指定的格式字符串和参数将格式化的字符串写入此输出流的便利方法。
|
void |
println()
通过写入行分隔符字符串来终止当前行。
|
void |
println(boolean x)
打印一个布尔值,然后终止该行。
|
void |
println(char x)
打印一个字符,然后终止该行。
|
void |
println(char[] x)
打印一个字符数组,然后终止该行。
|
void |
println(double x)
打印一次,然后终止行。
|
void |
println(float x)
打印一个浮点数,然后终止该行。
|
void |
println(int x)
打印一个整数,然后终止行。
|
void |
println(long x)
打印很长时间,然后终止行。
|
void |
println(Object x)
打印一个对象,然后终止该行。
|
void |
println(String x)
打印一个字符串,然后终止行。
|
protected void |
setError()
将流的错误状态设置为
true 。
|
void |
write(byte[] buf, int off, int len)
从指定的字节数组写入
len个字节,从偏移
off开始到此流。
|
void |
write(int b)
将指定的字节写入此流。
|
writepublic PrintStream(OutputStream out)
out - 要打印值和对象的输出流
PrintWriter.PrintWriter(java.io.OutputStream)
public PrintStream(OutputStream out, boolean autoFlush)
out - 要打印值和对象的输出流
autoFlush - 一个布尔值
如果为真,则每当写入一个字节数组时,输出缓冲区将被刷新,其中println方法之一被调用,或换行字符( '\n' )被写入
PrintWriter.PrintWriter(java.io.OutputStream, boolean)
public PrintStream(OutputStream out, boolean autoFlush, String encoding) throws UnsupportedEncodingException
out - 要打印值和对象的输出流
autoFlush - 一个布尔值
如果为真,每当写入一个字节数组时,输出缓冲区将被刷新,其中一个println方法被调用,或换行字符( '\n' )被写入
encoding - 支持的名称
character encoding
UnsupportedEncodingException - 如果不支持命名编码
public PrintStream(String fileName) throws FileNotFoundException
OutputStreamWriter ,它将使用Java虚拟机的这个实例使用default charset对字符进行编码。
fileName - 要用作此打印流的目标位置的文件的名称。
如果文件存在,那么它将被截断为零大小;
否则,将创建一个新文件。
输出将被写入文件并进行缓冲。
FileNotFoundException - 如果给定的文件对象不表示现有的可写常规文件,并且无法创建该名称的新常规文件,或者在打开或创建文件时出现其他错误
SecurityException - 如果存在安全管理员,并且
checkWrite(fileName)拒绝对该文件的写入访问
public PrintStream(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException
OutputStreamWriter ,它将使用提供的字符集对字符进行编码。
fileName - 要用作此打印流的目标位置的文件的名称。
如果文件存在,那么它将被截断为零大小;
否则,将创建一个新文件。
输出将被写入文件并进行缓冲。
csn - 支持的名称charset
FileNotFoundException - 如果给定的文件对象不表示现有的可写常规文件,并且无法创建该名称的新常规文件,或者在打开或创建文件时发生其他错误
SecurityException - 如果安全管理器存在,并且
checkWrite(fileName)拒绝对该文件的写入访问
UnsupportedEncodingException - 如果不支持命名的字符集
public PrintStream(File file) throws FileNotFoundException
OutputStreamWriter ,它将使用Java虚拟机的这个实例使用default charset对字符进行编码。
file - 用作此打印流file的文件。
如果文件存在,那么它将被截断为零大小;
否则,将创建一个新文件。
输出将被写入文件并进行缓冲。
FileNotFoundException - 如果给定的文件对象不表示现有的可写常规文件,并且无法创建该名称的新常规文件,或者在打开或创建文件时出现其他错误
SecurityException - 如果安全管理器存在,并且
checkWrite(file.getPath())拒绝对该文件的写访问
public PrintStream(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException
OutputStreamWriter ,它将使用提供的字符集对字符进行编码。
file - 用作此打印流file的文件。
如果文件存在,那么它将被截断为零大小;
否则,将创建一个新文件。
输出将被写入文件并进行缓冲。
csn - 支持的名称charset
FileNotFoundException - 如果给定的文件对象不表示现有的可写的常规文件,并且无法创建该名称的新常规文件,或者在打开或创建文件时发生其他错误
SecurityException - 如果存在安全管理员,并且
checkWrite(file.getPath())拒绝对文件的写入访问
UnsupportedEncodingException - 如果不支持命名的字符集
public void flush()
flush在界面
Flushable
flush在
FilterOutputStream
OutputStream.flush()
public void close()
close在界面
Closeable
close在界面
AutoCloseable
close在类别
FilterOutputStream
OutputStream.close()
public boolean checkError()
true当底层输出流引发IOException以外的InterruptedIOException ,当调用setError方法时。
如果底层输出流上的一个InterruptedIOException引发了一个InterruptedIOException ,那么PrintStream会通过执行以下PrintStream将PrintStream转换为中断:
Thread.currentThread().interrupt();
或等同物。
true当且仅当此流遇到
IOException以外的
InterruptedIOException或
setError方法被调用
protected void setError()
true 。
此方法将导致后续调用checkError()返回true直到clearError()被调用。
protected void clearError()
此方法将导致后续调用checkError()返回false直到另一个写操作失败并调用setError() 。
public void write(int b)
flush方法。
请注意,该字节写为给定; 根据平台的默认字符编码编写一个将被翻译的字符,使用print(char)或println(char)方法。
write在类别
FilterOutputStream
b - 要写入的字节
print(char) ,
println(char)
public void write(byte[] buf,
int off,
int len)
len字节,从偏移量off开始到此流。
如果启用自动冲洗,则将调用flush方法。
请注意,字节将按照给定的方式写入; 要根据平台的默认字符编码来编写将被翻译的字符,请使用print(char)或println(char)方法。
write在类别
FilterOutputStream
buf - 一个字节数组
off - 从
off开始读取字节的偏移量
len - 要写入的字节数
FilterOutputStream.write(int)
public void print(boolean b)
String.valueOf(boolean)生成的字符串根据平台的默认字符编码被翻译成字节,这些字节的写法方式完全符合write(int)要求。
b - 要打印的
boolean
public void print(char c)
write(int)完全按照write(int)方式编写。
c - 要打印的
char
public void print(int i)
String.valueOf(int)生成的字符串根据平台的默认字符编码被翻译成字节,这些字节是按照write(int)方法的方式编写的。
i - 要打印的
int
Integer.toString(int)
public void print(long l)
String.valueOf(long)生成的字符串根据平台的默认字符编码转换为字节,这些字节按照write(int)的方式写入。
l - 要打印的
long
Long.toString(long)
public void print(float f)
String.valueOf(float)生成的字符串根据平台的默认字符编码被翻译成字节,这些字节是按照write(int)方法的方式编写的。
f - 要打印的
float
Float.toString(float)
public void print(double d)
String.valueOf(double)生成的字符串根据平台的默认字符编码被翻译成字节,这些字节按照write(int)的方式写入。
d - 要打印的
double
Double.toString(double)
public void print(char[] s)
write(int)方法的方式编写的。
s - 要打印的字符数组
NullPointerException - 如果
s是
null
public void print(String s)
s - 要打印的
String
public void print(Object obj)
String.valueOf(Object)方法生成的字符串根据平台的默认字符编码进行转换为字节,这些字节的write(int)完全按照write(int)的方式编写。
obj - 要打印的
Object
Object.toString()
public void println()
line.separator定义,并不一定是单个换行符( '\n' )。
public void println(boolean x)
print(boolean)然后println() 。
x - 要打印的
boolean
public void println(char x)
print(char)然后println() 。
x - 要打印的
char 。
public void println(int x)
print(int)然后println() 。
x - 要打印的
int 。
public void println(long x)
print(long)然后println() 。
x - a要打印的
long 。
public void println(float x)
print(float)然后println() 。
x - 要打印的
float 。
public void println(double x)
print(double)然后println() 。
x - 要打印的
double 。
public void println(char[] x)
print(char[])然后println() 。
x - 要打印的字符数组。
public void println(String x)
print(String)然后println() 。
x - 要打印的
String 。
public void println(Object x)
print(String)然后println() 。
x - 要打印的
Object 。
public PrintStream printf(String format, Object... args)
调用此方法的形式out.printf(format, args)的行为方式与调用完全相同
out.format(format, args)
format -如在描述的格式字符串
Format string syntax
args - 格式字符串中格式说明符引用的参数。
如果比格式说明符更多的参数,额外的参数将被忽略。
参数的数量是可变的,可能为零。
参数的最大数量受限于The Java™ Virtual Machine Specification定义的Java数组的最大维数 。
一个null参数的行为取决于conversion 。
IllegalFormatException - 如果格式字符串包含非法语法,则与给定参数不兼容的格式说明符,给定格式字符串的参数不足或其他非法条件。
有关所有可能的格式化错误的规范 ,请参阅格式化程序类规范的Details部分。
NullPointerException - 如果
format是
null
public PrintStream printf(Locale l, String format, Object... args)
调用此方法的形式out.printf(l, format, args)的行为方式与调用完全相同
out.format(l, format, args)
l - 格式化期间应用的locale。
如果l是null ,则不应用本地化。
format - Format string syntax中
描述的格式字符串
args - 格式字符串中格式说明符引用的参数。
如果比格式说明符更多的参数,额外的参数将被忽略。
参数的数量是可变的,可能为零。
参数的最大数量受到The Java™ Virtual Machine Specification定义的Java数组的最大维度的限制 。
一个null参数的行为取决于conversion 。
IllegalFormatException - 如果格式字符串包含非法语法,则与给定参数不兼容的格式说明符,给定格式字符串的参数不足或其他非法条件。
有关所有可能的格式化错误的规范 ,请参阅格式化程序类规范的Details部分。
NullPointerException - 如果
format是
null
public PrintStream format(String format, Object... args)
始终使用的区域设置是由Locale.getDefault()返回的区域设置,无论以前在此对象上调用其他格式化方法。
format - Format string syntax中
描述的格式字符串
args - 格式字符串中格式说明符引用的参数。
如果比格式说明符更多的参数,额外的参数将被忽略。
参数的数量是可变的,可能为零。
参数的最大数量受限于The Java™ Virtual Machine Specification定义的Java数组的最大维数 。
一个null参数的行为取决于conversion 。
IllegalFormatException - 如果格式字符串包含非法语法,则与给定参数不兼容的格式说明符,给定格式字符串的参数不足或其他非法条件。
有关所有可能的格式化错误的规范 ,请参阅格式化程序类规范的Details部分。
NullPointerException - 如果
format是
null
public PrintStream format(Locale l, String format, Object... args)
l - 格式化期间应用的locale。
如果l是null ,则不应用本地化。
format - Format string syntax中
描述的格式字符串
args - 格式字符串中格式说明符引用的参数。
如果比格式说明符更多的参数,额外的参数将被忽略。
参数的数量是可变的,可能为零。
参数的最大数量受限于The Java™ Virtual Machine Specification定义的Java数组的最大维度 。
一个null参数的行为取决于conversion 。
IllegalFormatException - 如果格式字符串包含非法语法,与给定参数不兼容的格式说明符,给定格式字符串的参数不足或其他非法条件。
有关所有可能的格式化错误的规范 ,请参阅格式化程序类规范的Details部分。
NullPointerException - 如果
format是
null
public PrintStream append(CharSequence csq)
调用此方法的形式out.append(csq)的行为方式与调用完全相同
out.print(csq.toString())
取决于toString字符序列csq本说明书中,整个序列可以不追加。 例如,然后调用一个字符缓冲区的toString方法将返回一个序列,其内容取决于缓冲区的位置和限制。
append在界面
Appendable
csq - 要附加的字符序列。
如果csq是null ,则四个字符"null"附加到该输出流。
public PrintStream append(CharSequence csq, int start, int end)
形式的这种方法的调用时out.append(csq, start, end) csq不是null,行为以完全相同的方式调用
out.print(csq.subSequence(start, end).toString())
append在界面
Appendable
csq - 附加子序列的字符序列。
如果csq是null ,则会附加字符 ,如果csq包含四个字符"null" 。
start - 子序列中第一个字符的索引
end - 子序列中最后一个字符后面的字符的索引
IndexOutOfBoundsException - 如果
start或
end为负数,则
start大于
end ,或
end大于
csq.length()
public PrintStream append(char c)
调用此方法的形式out.append(c)的行为方式与调用完全相同
out.print(c)
append在界面
Appendable
c - 要追加的16位字符
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.