日期:2014-05-20  浏览次数:20908 次

关于flush的问题
import java.io.*;

public class TestIO{
private static BufferedReader stdIn=new BufferedReader(new InputStreamReader(System.in));
4. private static PrintWriter stdOut=new PrintWriter(System.out,true);
5. private static PrintWriter stdErr=new PrintWriter(System.err,true);

public static void main(String args[]) throws IOException{
1. stdOut.print("Source filename: ");
//stdOut.flush();

BufferedReader input=new BufferedReader(new FileReader(stdIn.readLine()));

2. stdErr.print("destination filename: ");
//stdErr.flush();

PrintWriter output=new PrintWriter(new FileWriter(stdIn.readLine()));

String line=input.readLine();
while(line!=null){
output.println(line);
line=input.readLine();
}

input.close();
output.close();

3. stdOut.println("done");
//stdOut.flush();
}

}
如果注释了flush的行,则标记为1和2的语句不输出,但是3是输出的,为什么?
另外如果标记为4和5的地方最后不加true,则3也不输出,看api说加true是代表自动fulsh,但是不对啊。。不加fulsh不行

------解决方案--------------------
你没仔细看,
autoFlush - boolean 变量;如果为 true,则 println、printf 或 format 方法将刷新输出缓冲区。
你调用的是print,所以没有用。