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
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.
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.