System.out.write和System.out.println

    技术2022-05-19  20

    来源:http://zhidao.baidu.com/question/59412568

     

    Q :

    System.out.write是字符流,System.out.println一个是字节流先说System.out.writeclass A{     public static void main(String[] args)     {         char a='a';         System.out.write(a);     }}  在控制台什么都看不到,但是

    class A { public static void main(String[] args) { char a='a'; System.out.write(a); System.out.write('/n'); } } 这样为什么能在控制台输出a来? 再说下System.out.println class A { public static void main(String[] args) { char a='a'; System.out.println('a'); } } 在控制台上输出的是a class A { public static void main(String[] args) { char a='a'; System.out.println(a); } } 在控制台上输出的是97

    问题补充:

    第2个问题两个结果都是a;我想知道下,PrintStream带缓冲,那么print为什么能直接输出来

    A : System.out的类型为PrintStream; System.out.println('a'); 实际上调用是PrintStream的println(char c)方法;而println(char c)方法的源代码为: public void println(String x) { synchronized (this) { print(x); newLine(); } } 可见Println调用了print(char c)方法,print(char c)方法的源代码如下: public void print(char c) { write(String.valueOf(c)); } 可见调用的是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()的代码如下: private void newLine() { try { synchronized (this) { ensureOpen(); textOut.newLine(); textOut.flushBuffer(); charOut.flushBuffer(); if (autoFlush) out.flush(); } } catch (InterruptedIOException x) { Thread.currentThread().interrupt(); } catch (IOException x) { trouble = true; } } 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',也同样会直接输出出来。

    最新回复(0)