下面是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);
}
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:中的out.flush()就不会被执行
可是结果却出人意外,为什么它flush()了呢?
new BufferedOutputStream(new FileOutputStream("D:\\333\\111.txt")), true);
把这里面的true去掉试试,应该就会没反应了
textOut.write(buf);
textOut.flushBuffer();
charOut.flushBuffer();
这三句话动了手脚,但是我找了半天,也没有“证据”