我用autoflush创建了一个PrintWriter; 为什么不自动进行?

我的客户端是一个Web浏览器,并使用以下URL发送请求到myserver: http://localhost

这是服务器端代码。 问题在于ServingThread类的run方法。

 class ServingThread implements Runnable{ private Socket socket ; public ServingThread(Socket socket){ this.socket = socket ; System.out.println("Receives a new browser request from " + socket + "\n\n"); } public void run() { PrintWriter out = null ; try { String str = "" ; out = new PrintWriter( socket.getOutputStream() ) ; out.write("This a web-page.") ; // :-( out.flush() ; // :-( socket.close() ; System.out.println("Request successfully fulfilled.") ; } catch (IOException io) { System.out.println(io.getMessage()); } } } 

我是否正在使用

 out = new PrintWriter( socket.getOutputStream(), true ) ; 

要么

 out = new PrintWriter( socket.getOutputStream() ) ; 

输出没有进入浏览器。 仅当我使用流手动刷新时,输出才会进入浏览器

 out.flush() ; 

我的问题: new PrintWriter( socket.getOutputStream(), true )应该自动刷新输出缓冲区,但它没有这样做。 为什么?

来自Javadocs :

参数:

out – 输出流
autoFlush – 布尔值; 如果为true,则printlnprintfformat方法将刷新输出缓冲区

它并没有说write()将刷新输出缓冲区。 尝试使用println()代替它,它应该像你期望的那样刷新。