这行代码我尝试读源码,发现这样一段
//FilterOutputStream.java
public void write(int b) throws IOException {
out.write(b);
}
进一步跟踪
//OutputStream.java
private void ensureOpen() throws IOException {
if (this.closed) {
throw new IOException("Stream closed");
}
}
public void write(int b) throws IOException {
this.ensureOpen();
}
然而这段代码并没有使用参数b。有没有大佬能解答我的疑问。
不知道你看的是哪个版本的,我在JDK8看到的是这样的(PrintStream.java
):
/**
* Writes the specified byte to this stream. If the byte is a newline and
* automatic flushing is enabled then the <code>flush</code> method will be
* invoked.
*
* <p> Note that the byte is written as given; to write a character that
* will be translated according to the platform's default character
* encoding, use the <code>print(char)</code> or <code>println(char)</code>
* methods.
*
* @param b The byte to be written
* @see #print(char)
* @see #println(char)
*/
public void write(int b) {
try {
synchronized (this) {
ensureOpen();
out.write(b);
if ((b == '\n') && autoFlush)
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}