• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BufferedWriter

 
kriti sharma
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I use BufferedWriter, the server program keeps on waiting, that means BufferedWriter does not write.why???PrintWriter does write and my program works.
import java.io.*;
import java.net.*;
class Aa
{
public static void main(String str[]) throws Exception
{
Socket s=new Socket("localhost",33333);
int i=5;
// BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
while(true)
{
for(i=5;i>=0;i--)
{
out.println(i+"message from Client");
//bw.write(�message sent to server�);
//bw.flush();
System.out.println("message sent to server");
}

}

}
}
[ April 18, 2004: Message edited by: kriti sharma ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably need to flush() the stream. Or if you're done, simply close() it. Or you can make a PrintStream or PrintWriter which does auto-flush when you call println() - that's what System.out does. See the API for PrintStream and PrintWriter, and look at the constructors that take a boolean argument.
 
kriti sharma
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did flush and close the BufferedWriter stream but it does not write to the stream.the server program keeps on waiting.PrintWriter works very well.why is this happening?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic