This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes problem in System.out.write Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem in System.out.write" Watch "problem in System.out.write" New topic
Author

problem in System.out.write

gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
hi,
i'm reading files from a zip file one by one and dispalying the o/p on the console in which a starnge thing happens.
when i use System.out.write(x), it works fine but doesn't prints the last line of last file, but with System.out.print(char(x)), it works perfectly fine.
i know System.out.write(x) writes bytes to stream, but in any case it should not skip the whole line.
i'm baffled.


thanks,<br />gaurav abbi
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You probably just need to call System.out.flush() at the end of your program.


[Jess in Action][AskingGoodQuestions]
gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
hi Ernest,
thanks a lot, it worked, please tell me the reason...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Basically, Java doesn't guarantee that output is actually sent to a file, socket, the screen, or any other output device until you call flush() on your OutputStream or Writer, or until it is closed. The OutputStream or Writer may buffer the output, meaning that it will be saved up and sent in larger chunks for efficiency. You can't really predict how this will be done.

PrintWriter.println() will automatically call flush(), by the way, although that doesn't matter here. But in any case, the general rule is that if you want output to appear somewhere immediately, and you're not using PrintWriter.println() (or PrintStream.println()), then call flush() yourself.
gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
thanks, i got it...
Sven Turnau
Greenhorn

Joined: Feb 07, 2009
Posts: 2
I have to write to the terminal ESC character. It doesn't work, but if I write instead of 27 ex. 51 it works.
System.out.write((byte)(27));Is NOK! System.out.write((byte)(51)); is OK!
I have another problem too: How to read from console one character without waiting for <CR>.
Thanks a lot.

private void CSI() { // Control Sequence Initiator
System.out.write((byte)(27));
System.out.print("["); // (27+"["); // ESC[
}
Sven Turnau
Greenhorn

Joined: Feb 07, 2009
Posts: 2
System.out.write((int)(27)); Expected to output in Java the ESC symbol, but the system
catch the ESC symbol. Actually we needed to output two characters 'ESC' and '['
we can work this problem around instead of writing out two characters we can
output one character with the code (int) 155. See wikipedia.
System.out.print((char)(155));
So the problem was been solved. In "C" no characters will be catched. Why this is done
in Java I only can guess.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem in System.out.write