Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PrintStream constructor deprecated :(

Posted on 1998-02-03
6
Medium Priority
?
509 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

688 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