Solved

PrintStream constructor deprecated :(

Posted on 1998-02-03
6
498 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:musashi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Author Comment

by:musashi
ID: 1232826
Edited text of question
0
 
LVL 1

Expert Comment

by:froderik
ID: 1232827
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
0
 

Author Comment

by:musashi
ID: 1232828
>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?

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:gwalters
ID: 1232829
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.

0
 

Author Comment

by:musashi
ID: 1232830
>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.
0
 
LVL 1

Accepted Solution

by:
froderik earned 100 total points
ID: 1232831
I have to admit that I mostly answered the latter part of the question. It is really strange design, to require a deprecated class as argument in a new method. They were out of time, I guess.

If you really want to use PrintWriter instead of PrintStream there is at least one way: subclass PrintStream. Take a PrintWriter object as argument to the constructor and simply implement all methods from the superclass to point to the constructor argument. I would say that it is too much work...

public class PrintStreamWriter extends PrintStream
{
  private PrintWriter myWriter;

  public PrintStreamWriter( PrintWriter aWriter )
  {
    this.myWriter = aWriter;
  }

  public print( String s )
  {
    myWriter.print( s );
  }

  // And so on..
}
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question