Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > a Java io question -- disable line buffering of PrintStream

Reply
Thread Tools

a Java io question -- disable line buffering of PrintStream

 
 
www
Guest
Posts: n/a
 
      01-30-2007
Hi,

I am reading Sun's tutorial
page.(http://java.sun.com/developer/techni...ng/PerfTuning/)

<Quote>
....
Another aspect of buffering concerns text output to a terminal window.
By default, System.out (a PrintStream) is line buffered, meaning that
the output buffer is flushed when a newline character is encountered.
This is important for interactivity, where you'd like to have an input
prompt displayed before actually entering any input.
Approach 5: Disabling Line Buffering

But line buffering can be disabled, as in this example:



import java.io.*;

public class bufout {
public static void main(String args[]) {
FileOutputStream fdout =
new FileOutputStream(FileDescriptor.out);
BufferedOutputStream bos =
new BufferedOutputStream(fdout, 1024);
PrintStream ps =
new PrintStream(bos, false);

System.setOut(ps);

final int N = 100000;

for (int i = 1; i <= N; i++)
System.out.println(i);

ps.close();
}
}


This program writes the integers 1..100000 to the output, and runs about
three times faster than the default equivalent that has line buffering
enabled.
</Quote>

The above code really bring me to trouble. Particularly, ps.close() does
strange thing. It seems to shut down my JVM or other similar end result.
All the code after ps.close() will not be reached. For example, I have
added the code:


ps.close()
System.setOut(System.out);
System.out.println("hello"); //never reach here

Thank you for your help.
 
Reply With Quote
 
 
 
 
www
Guest
Posts: n/a
 
      01-30-2007
www wrote:
> For example, I have
> added the code:
>
>
> ps.close()
> System.setOut(System.out);
> System.out.println("hello"); //never reach here
>



Sorry. I already found the clause. I need to store System.out in a
temporary holder, then later put it back:

PrintStream temp = System.out;
System.setOut(ps);

final int N = 100000;

for (int i = 1; i <= N; i++)
System.out.println(i);

ps.close();
System.setOut(temp); //gets back the original PrintStream
System.out.println("Are you ok?"); //now it is printed out
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where Do PrintStream Exceptions Go? Lawrence D'Oliveiro Java 4 03-28-2011 06:23 PM
why System.out is wrapped with PrintStream but System.in not? Evol Java 5 09-03-2009 03:56 AM
Re: PrintStream to Writer Harald Hein Java 2 08-20-2003 09:02 PM
java.io.BufferedWriter and java.io.PrintStream - a seeming parameter/argument mismatch in method invocation Novice Java 1 07-19-2003 06:46 PM
trying to disable double buffering Paul Watkins Computer Support 14 06-27-2003 01:30 AM



Advertisments