Solved

PrintStream constructor deprecated :(

Posted on 1998-02-03
6
476 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
java set up 1 48
wordcount challenge 11 78
HashMap Vs TreeMap 12 49
solarwind tftp server 2 32
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now