📄 javaio.doc20.html
字号:
<html>
<head>
<title>The Java Language Specification The Package java.io </title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<a href="index.html">Contents</a> | <a href="javaio.doc19.html">Prev</a> | <a href="javaio.doc21.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="29906"></a>
<center><h1>22.22 The Class <code>java.io.PrintStream</code></h1></center>
<a name="29907"></a>
A <code>PrintStream</code> adds functionality to another output stream-namely, the ability
to print representations of various data values conveniently. Two other features are
provided as well. Unlike other output streams, a <code>PrintStream</code> never throws an
<code>IOException</code>; instead, exceptional situations merely set an internal flag that can
be tested by the <code>checkError</code> method. Optionally, a <code>PrintStream</code> can be created
so as to "autoflush"; this means that after an array of bytes is written, or after a
single byte equal to <code>'\n'</code> is written, the <code>flush</code> method is automatically invoked.
<p><pre><a name="29908"></a>public class <code><b>PrintStream</b></code> extends FilterOutputStream {
<a name="29909"></a> public <code><b>PrintStream</b></code>(OutputStream out);
<a name="29910"></a> public <code><b>PrintStream</b></code>(OutputStream out, boolean autoflush);
<a name="29911"></a> public void <code><b>write</b></code>(int b);
<a name="29912"></a> public void <code><b>write</b></code>(byte[] b, int off, int len)
<a name="32497"></a> throws NullPointerException, IndexOutOfBoundsException;
<a name="29913"></a> public void <code><b>flush</b></code>();
<a name="29914"></a> public void <code><b>close</b></code>();
<a name="29915"></a> public boolean <code><b>checkError</b></code>();
<a name="29916"></a> public void <code><b>print</b></code>(Object obj);
<a name="29917"></a> public void <code><b>print</b></code>(String s);
<a name="29918"></a> public void <code><b>print</b></code>(char[] s) throws NullPointerException;
<a name="46178"></a> public void <code><b>print</b></code>(boolean b);
<a name="29919"></a> public void <code><b>print</b></code>(char c);
<a name="29920"></a> public void <code><b>print</b></code>(int i);
<a name="29921"></a> public void <code><b>print</b></code>(long l);
<a name="29922"></a> public void <code><b>print</b></code>(float f);
<a name="29923"></a> public void <code><b>print</b></code>(double d);
<a name="29925"></a> public void <code><b>println</b></code>();
<a name="29926"></a> public void <code><b>println</b></code>(Object obj);
<a name="31652"></a> public void <code><b>println</b></code>(String s);
<a name="46170"></a> public void <code><b>println</b></code>(char[] s) throws NullPointerException;
<a name="46174"></a> public void <code><b>println</b></code>(boolean b);
<a name="46171"></a> public void <code><b>println</b></code>(char c);
<a name="46172"></a> public void <code><b>println</b></code>(int i);
<a name="29931"></a> public void <code><b>println</b></code>(long l);
<a name="29932"></a> public void <code><b>println</b></code>(float f);
<a name="29933"></a> public void <code><b>println</b></code>(double d);
<a name="29935"></a>}
</pre><a name="29936"></a>
<p><font size=+1><strong>22.22.1 </strong> <code>public <code><b>PrintStream</b></code>(OutputStream out)</code></font>
<p>
<a name="29937"></a>
This constructor initializes a newly created <code>PrintStream</code> by saving its argument,
the output stream <code>out</code>, for later use. This stream will not autoflush.
<p><a name="29941"></a>
<p><font size=+1><strong>22.22.2 </strong> <code>public <code><b>PrintStream</b></code>(OutputStream out, boolean autoflush)</code></font>
<p>
<a name="29942"></a>
This constructor initializes a newly created <code>PrintStream</code> by saving its argument,
the output stream <code>out</code>, for later use. This stream will autoflush if and only if
<code>autoflush</code> is <code>true</code>.
<p><a name="29947"></a>
<p><font size=+1><strong>22.22.3 </strong> <code>public void <code><b>write</b></code>(int b)</code></font>
<p>
<a name="29951"></a>
See the general contract of the <code>write</code> method of <code>OutputStream</code> <a href="javaio.doc13.html#29411">(§22.15.1)</a>.
<p><a name="29955"></a>
Overrides the <code>write</code> method of <code>FilterOutputStream</code> <a href="javaio.doc17.html#29666">(§22.19.3)</a>.<p>
<a name="29956"></a>
<p><font size=+1><strong>22.22.4 </strong> <code>public void <code><b>write</b></code>(byte[] b, int off, int len)<br>throws NullPointerException,      IndexOutOfBoundsException</code></font>
<p>
<a name="29960"></a>
See the general contract of the <code>write</code> method of <code>OutputStream</code> <a href="javaio.doc13.html#29426">(§22.15.3)</a>.
<p><a name="29964"></a>
Overrides the <code>write</code> method of <code>FilterOutputStream</code> <a href="javaio.doc17.html#29680">(§22.19.5)</a>.<p>
<a name="29965"></a>
<p><font size=+1><strong>22.22.5 </strong> <code>public void <code><b>flush</b></code>()</code></font>
<p>
<a name="29969"></a>
See the general contract of the <code>flush</code> method of <code>OutputStream</code> <a href="javaio.doc13.html#29440">(§22.15.4)</a>.
<p><a name="29973"></a>
Overrides the <code>flush</code> method of <code>FilterOutputStream</code> <a href="javaio.doc17.html#29687">(§22.19.6)</a>.<p>
<a name="29974"></a>
<p><font size=+1><strong>22.22.6 </strong> <code>public void <code><b>close</b></code>()</code></font>
<p>
<a name="29978"></a>
See the general contract of the <code>close</code> method of <code>OutputStream</code> <a href="javaio.doc13.html#29445">(§22.15.5)</a>.
<p><a name="29982"></a>
Overrides the <code>close</code> method of <code>FilterOutputStream</code> <a href="javaio.doc17.html#29694">(§22.19.7)</a>.<p>
<a name="29983"></a>
<p><font size=+1><strong>22.22.7 </strong> <code>public boolean <code><b>checkError</b></code>()</code></font>
<p>
<a name="29984"></a>
The result is <code>true</code> if and only if this output stream has ever encountered any kind
of trouble-that is, if any operation on the contained output stream has ever
resulted in an <code>IOException</code> other than an <code>InterruptedIOException</code>. If an
operation  on the contained output stream throws an <code>InterruptedIOException</code>,
then the <code>PrintStream</code> class converts the exception back to an interrupt by doing:
<p><pre><a name="29985"></a>Thread.currentThread().interrupt();
</pre><a name="29986"></a>
or the equivalent.
<p><a name="29988"></a>
<p><font size=+1><strong>22.22.8 </strong> <code>public void <code><b>print</b></code>(Object obj)</code></font>
<p>
<a name="29989"></a>
The low-order bytes of the characters in the <code>String</code> that would be produced by
<code>String.valueOf(obj)</code> <a href="javalang.doc11.html#14015">(§20.12.38)</a> are written, in order, to the contained output
stream in exactly the manner of the <code>write</code> method <a href="javaio.doc20.html#29947">(§22.22.3)</a>.
<p><a name="29997"></a>
<p><font size=+1><strong>22.22.9 </strong> <code>public void <code><b>print</b></code>(String s)</code></font>
<p>
<a name="29998"></a>
The low-order bytes of the characters in the string <code>s</code> are written, in order, to the
contained output stream in exactly the manner of the <code>write</code> method <a href="javaio.doc20.html#29947">(§22.22.3)</a>.
If  <code>s</code> is <code>null</code>, then the low-order bytes of the four characters <code>n</code>, <code>u</code>, <code>l</code>, <code>l</code> are written to
the contained output stream.
<p><a name="30003"></a>
<p><font size=+1><strong>22.22.10 </strong> <code>public void <code><b>print</b></code>(char[] s) throws <code>NullPointerException</code></code></font>
<p>
<a name="31623"></a>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -