java.io question

HI

could you please tell what's wrong with the following code. I would like to read from system and write it back. I'm able to read but couldn't write...!

import java.io.*;

class SystemDemo

{

publicstaticvoid main(String[] args)throws Exception

{

InputStream is = System.in;

PrintWriter pw =new PrintWriter(System.out);

int c;

while ( (c = is.read()) != -1){

pw.write(c);}

pw.close();

}

}

Thanks

[953 byte] By [nalajala2a] at [2007-10-3 2:47:39]
# 1
In your program, it won't write to the output till it finishes reading the input.
el_doradoa at 2007-7-14 20:36:26 > top of Java-index,Java Essentials,Java Programming...
# 2

No, you have it backwards. YOU are supposed to tell us what's wrong with it, and WE help you fix it.

So. What's wrong with it? Does it not compile? If so, tell us what the compiler errors are. Or does it throw an exception when you run it? If so, post the stack trace for us. Or does it just not do what you expected it to do? If so, explain what you expected it to do and what it actually did.

DrClapa at 2007-7-14 20:36:26 > top of Java-index,Java Essentials,Java Programming...
# 3
Try executing your program from command line and press Ctrl-C when you are tired of pressing the input. You will see the output.
el_doradoa at 2007-7-14 20:36:26 > top of Java-index,Java Essentials,Java Programming...
# 4

You need to add a call to pw.flush(); to get the output to write out.

You can do it after every character but its probably better to do only it when you need to: after every new line in the input, for example.

Most likely none of your output will show up until you hit return on the input stream.

JohnKeltya at 2007-7-14 20:36:26 > top of Java-index,Java Essentials,Java Programming...