System.out.write VS System.out.print()
来源:互联网 发布:傅园慧走红网络 编辑:程序博客网 时间:2024/04/30 05:52
System.out.write(int a):输出字节;
System.out.print(int a):输出字符。
摘要:println(String s)不但会刷新out,而且还会同时刷新textOut和charOut,而write(int b)只有当b == '/n'时才刷新out。这也是为什么加了System.out.write('/n')后就能显示出来了,问题就在于out没有刷新。在print(String s)/print(int a)中,会刷新textOut和charOut。textOut和charOut操作的输出流和out是一样的,因此对textOut和charOut刷新同时刷新了out,因此print(String s)即便没有'/n',也同样会直接输出出来。
print(int a)的源码如下:
public void print(int i) {
write(String.valueOf(i));
}
可见调用的是write(String s)方法,write(String s)的代码为:
System.out.print(int a):输出字符。
摘要:println(String s)不但会刷新out,而且还会同时刷新textOut和charOut,而write(int b)只有当b == '/n'时才刷新out。这也是为什么加了System.out.write('/n')后就能显示出来了,问题就在于out没有刷新。在print(String s)/print(int a)中,会刷新textOut和charOut。textOut和charOut操作的输出流和out是一样的,因此对textOut和charOut刷新同时刷新了out,因此print(String s)即便没有'/n',也同样会直接输出出来。
print(int a)的源码如下:
public void print(int i) {
write(String.valueOf(i));
}
可见调用的是write(String s)方法,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;
}
}
当字符串中含有'/n'时会刷新out,此处的out是OutStream对象的实例。println(String s)最后调用newLine() 方法,newLine()会刷新out。
System.out.write(a); 调用的是PrintStream.write(int b)方法,write(int b) 的源代码如下:
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;
}
}
看过源代码后应该明白两者之间的差异了,println(String s)不但会刷新out,而且还会同时刷新textOut和charOut,而write(int b)只有当b == '/n'时才刷新out。这也是为什么加了System.out.write('/n')后就能显示出来了,问题就在于out没有刷新。
在print(String s)中,会刷新textOut和charOut。
textOut和charOut是什么?看一下PrintStream中的定义:
private BufferedWriter textOut;
private OutputStreamWriter charOut;
textOut和charOut在init(OutputStreamWriter osw)方法中初始化,init(OutputStreamWriter osw)的代码如下:
private void init(OutputStreamWriter osw) {
this.charOut = osw;
this.textOut = new BufferedWriter(osw);
}
init()函数在构造函数中被调用
public PrintStream(OutputStream out, boolean autoFlush) {
this(autoFlush, out);
init(new OutputStreamWriter(this));
}
可见,textOut和charOut操作的输出流和out是一样的,因此对textOut和charOut刷新同时刷新了out,因此print(String s)即便没有'/n',也同样会直接输出出来。
- System.out.write VS System.out.print()
- System.out.write()和System.out.print()问题
- 简化System.out.print
- 浅谈System.out.print();
- System.out.print(obj);
- System.out.print("我爱你")
- System.out.write和System.out.println
- System.out.write和System.out.println
- System.out.println()和System.out.write()
- system.out.print和log4j
- 直接调用system.out.print()
- 深入理解System.out.print
- System.out.print("Hello CSDN!")
- System.in.read(); System.out.write();
- System.out.println与System.out.print的区别
- System.out.print和System.out.println的区别
- System.out.print与System.out.println的区别
- System.out.println()与System.out.print("\n")的区别
- 十进制 转换八进制和二进制 十六进制 转换器
- 解答:输出1到1000中能被7整除或者以7结尾的数
- 白手起家的企业家
- 望明月----中秋随感
- 常见测试相关的软件工程名词解释
- System.out.write VS System.out.print()
- [转]VC 键盘虚拟码
- 【转】计算机键盘功能键作用
- 一朵白花 ——《紫日》观后感
- window.open
- 忙忙碌碌-碌碌无为
- 今天入住“CSDN”。。。继续前进
- 最近有点无聊
- 计算一个文本文件的行数