Link to home
Start Free TrialLog in
Avatar of musashi
musashi

asked on

PrintStream constructor deprecated :(

i want to use System.setOut( PrintStream out ) to re-assign System.out.

i dont see a non-deprecated way of obtaining an instance of PrintStream that i need.

any ideas?

currently i am using :

    ByteArrayOutputStream outputByteArray = new ByteArrayOutputStream();
    PrintStream debugOutputStream = new PrintStream( outputByteArray, true );
    PrintStream oldOut = System.out;
    System.setOut( debugOutputStream );

but the PrintStream constructor brings up a warning that it is deprecated.

thanks

musashi
Avatar of musashi
musashi

ASKER

Edited text of question
The PrintStream class was replaced in Java 1.1 with PrintWriter. It has the same methods as PrintStream so you could just replace all ocurrences of PrintStream with PrintWriter. The only thing that is different is that there are two more constructors that take Writer objects as argument.

(There is no way to keep the PrintStream class, compile with Java 1.1 compiler and not recieving an deprecation warning. Your program should work anyway though. If the program will live and be maintained for a long time it could be wise to do the replacement. Otherwise it doesn't matter much, as long as the program works.)

Best regards

Fredrik
Avatar of musashi

ASKER

>The PrintStream class was replaced in Java 1.1 with PrintWriter.

i knew this and would use it if i could but System.setOut() will only take a "PrintStream".

if there was a way to convert a "PrintWriter" to a "PrintStream" then i wouldn't have any problems. as far as i have been able to figure out, you can convert an "OutputStream" to a "Writer" but not vice versa (which is what i need).

is there a way?

I agree with froderik.  There's nothing inherently wrong with using deprecated methods/classes, as long as you understand that your code may not work in a future version.  However, if some future version eliminates (as opposed to deprecating) PrintStream, you can be sure that System.setOut(PrintStream) will also be eliminated and, hopefully, replaced with System.setOut(PrintWriter).

What gets me is that System.setOut() was added for 1.1!  They tell you to use PrintWriter in all new code, and then they add new code that uses PrintStream.

Avatar of musashi

ASKER

>What gets me is that System.setOut() was added for 1.1! They tell you to use PrintWriter in all new code, and then they add new code that uses PrintStream.

yes i know, i think sun screwed up here.

anyway, froderik. submit something as an answer again and you can have the points.
ASKER CERTIFIED SOLUTION
Avatar of froderik
froderik

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