• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in System.out.write

 
gaurav abbi
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably just need to call System.out.flush() at the end of your program.
 
gaurav abbi
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ernest,
thanks a lot, it worked, please tell me the reason...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, i got it...
 
Sven Turnau
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic