Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

a Java io question -- disable line buffering of PrintStream

Thread view: 
www - 30 Jan 2007 18:41 GMT
Hi,

I am reading Sun's tutorial
page.(http://java.sun.com/developer/technicalArticles/Programming/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.
www - 30 Jan 2007 18:53 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.