Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PrintStream constructor deprecated :(

Posted on 1998-02-03
6
Medium Priority
?
516 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
  • 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 300 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

916 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