api中解释,PrintStream打印流的print(int a)方法是通过调用String.valueOf(int a)方法将a转换为String,再将String转换为字节数组,调用write(byte[])方法将字节数组中的字节写入目的设备中。但是查看print(int a)源码显示如下:
public void print(int i) {
write(String.valueOf(i));
}
//write(String s)的源码
private void write(String s) {
try {
synchronized (this) {
ensureOpen();
textOut.write(s);
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush && (s.indexOf('\n') >= 0))
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
源码中并不是按照write(byte[] s)写入的,而是直接用字符流的write(String s)写入的,这是为什么啊,该以哪个为准啊,求大神解惑,感谢!