Link to home
Start Free TrialLog in
Avatar of Steve34
Steve34Flag for United States of America

asked on

How To Redirect Standard Out

I would like to be able to redirect standard out in Java 1.1 to write directly to my local printer.

I would like to be able to do something like:

System.out.println( sMyStringToThePrinter );

and have it go to my printer.

Can this be done?  If so, how?  Could someone show me an example?  I don't know where to start.

ASKER CERTIFIED SOLUTION
Avatar of jpk041897
jpk041897

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Steve34

ASKER

I got some of that from the docs, but what would an implementation of it look like?
Avatar of jpk041897
jpk041897

Something in the lines of:

System.setOut(new PrintStream(new FileOutputStream("LPT1:")));

Change LPT1: to whatever port (LPTx:) your priter is hooked on to.

Note: Depending on weather you are using an applet or an application, some security restriction may apply. C.F. FileOutputStream for additional info.
You might also want to read the current open question at:

https://www.experts-exchange.com/Computers/Programming/Languages/Java/Q.10165426

for some additional insight.
Avatar of Steve34

ASKER

This is what I tried and I got an exception.  Any ideas what I am missing?  I actually do have a printer with a port of lpt1:

import java.util.*;
import java.text.*;
import java.lang.*;
import java.io.*;

public class HelloWorld
{
     public static void main( String[] args )
     {

      String sUnderlinedString = "I am underlined \r_______________";
 
      try
      {
                System.setOut(new PrintStream(new FileOutputStream("LPT1:")));
                System.out.print(sUnderlinedString);      
      }
      catch(IOException e)
      {
          System.err.println("IOException ");
      }  
      
     }// end main

}

Try changing LPT1: to PRN: wich redirects to the default printer.