下面是PrintStream的源代码,但是这个write方法我有点看不懂 private void write(char buf[]) {
try {
    synchronized (this) {
ensureOpen();
textOut.write(buf);
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush) {
    for (int i = 0; i < buf.length; i++)
if (buf[i] == '\n') //主要是这里我,如果我传进来的char[]里不含有'\n'依然会刷新,所以我有点不理解。请看下面的代码
    out.flush();
}
    }
}
catch (InterruptedIOException x) {
    Thread.currentThread().interrupt();
}
catch (IOException x) {
    trouble = true;
}
    }PrintStream out2 = new PrintStream(
new BufferedOutputStream(new FileOutputStream("D:\\333\\111.txt")), true);
char[] c = new char[]{'e', 'f', 'g', 'h'};
out2.print(c);
// out2.flush();  //看原代码应该是char[]并未含有'\n',那它是如何自动刷新的呢?

BufferedInputStream in = new BufferedInputStream(
new FileInputStream("D:\\333\\111.txt"));
int a = 0;
while((a = in.read()) != -1){
System.out.print((char)a);
}

解决方案 »

  1.   

    为了自动刷新,可以创建一个 PrintStream;这意味着可在写入 byte 数组之后自动调用 flush 方法,可调用其中一个 println 方法,或写入一个换行符或字节 ('\n')。 
      

  2.   

    flush()是手动刷新,即使不调用,JAVA也会在适当的时候自动刷新的。
      

  3.   

    代码段2:我把out2.flush()注释掉了,但是还是刷新了,但是从源代码中推断,应该如果没有'\n'这个字符。
    代码段1:中的out.flush()就不会被执行
    可是结果却出人意外,为什么它flush()了呢?
      

  4.   

    PrintStream out2 = new PrintStream(
                    new BufferedOutputStream(new FileOutputStream("D:\\333\\111.txt")), true);
    把这里面的true去掉试试,应该就会没反应了
      

  5.   

    回答楼上的,我当然知道aotoFlush参数为false时是不会自动刷新的,我奇怪的是,源代码中的for循环
      

  6.   

    会不会是
    textOut.write(buf);
    textOut.flushBuffer();
    charOut.flushBuffer();
    这三句话动了手脚,但是我找了半天,也没有“证据”