Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

File /dev/null in Java ?

Thread view: 
Jürgen Gerstacker - 27 Nov 2006 09:33 GMT
Hi,
is there a garbage file in Java like /dev/null in UNIX?
I want temporarily throw away System.out outputs.

PrintStream out_orig=System.out;
System.setOut(new PrintStream(new File("trash.txt")));
...
System.setOut(out_orig);

The file "trash.txt" is created always, but I don't need it (and don't
want it). What could I do?

Juergen
Jussi Piitulainen - 27 Nov 2006 09:57 GMT
> is there a garbage file in Java like /dev/null in UNIX?
> I want temporarily throw away System.out outputs.
[quoted text clipped - 6 lines]
> The file "trash.txt" is created always, but I don't need it (and
> don't want it). What could I do?

You could subclass PrintStream with methods that do nothing. It may be
enough to override its three write methods:

 public class NullPrintStream extends PrintStream {
     void write(byte[] buf, int off, int len) {}
     void write(int b) {}
     void write(byte [] b) {}
 }

Then just:

 System.setOut(new NullPrintStream());

I'm not sure, but this is how I would start.
Furious George - 27 Nov 2006 10:18 GMT
> > is there a garbage file in Java like /dev/null in UNIX?
> > I want temporarily throw away System.out outputs.
[quoted text clipped - 15 lines]
>       void write(byte [] b) {}
>   }

Good but better would be to subclass OutputStream.  It would be
sufficient to write one write method.  Then you could use it as an
argument to the PrintStream constructor

System . setOut ( new java . io . PrintStream ( new java . io .
OutputStream ( ) { public void write ( int b ) { } } ) ) ;

> Then just:
>
>   System.setOut(new NullPrintStream());
>
> I'm not sure, but this is how I would start.
Jussi Piitulainen - 27 Nov 2006 10:29 GMT
>>> is there a garbage file in Java like /dev/null in UNIX?
>>> I want temporarily throw away System.out outputs.
...
>> You could subclass PrintStream with methods that do nothing. It may be
>> enough to override its three write methods:
[quoted text clipped - 11 lines]
> System . setOut ( new java . io . PrintStream ( new java . io .
> OutputStream ( ) { public void write ( int b ) { } } ) ) ;

Nice. That may be the solution.
Jürgen Gerstacker - 27 Nov 2006 14:38 GMT
> System . setOut ( new java . io . PrintStream ( new java . io .
> OutputStream ( ) { public void write ( int b ) { } } ) ) ;

This works greatly. Thank you!
 Juergen
Furious George - 27 Nov 2006 14:46 GMT
J?rgen Gerstacker wrote:
> > System . setOut ( new java . io . PrintStream ( new java . io .
> > OutputStream ( ) { public void write ( int b ) { } } ) ) ;
>
> This works greatly. Thank you!
>   Juergen

Bitte!
Oliver Wong - 27 Nov 2006 16:07 GMT
> Hi,
> is there a garbage file in Java like /dev/null in UNIX?
[quoted text clipped - 7 lines]
> The file "trash.txt" is created always, but I don't need it (and don't
> want it). What could I do?

Did you try this?

 System.setOut(null);

I believe I did that a long time ago, and it seemed to work (silenced the
output, didn't throw null pointer exceptions).

   - Oliver
Jussi Piitulainen - 27 Nov 2006 16:21 GMT
>> is there a garbage file in Java like /dev/null in UNIX?
>> I want temporarily throw away System.out outputs.
...
> Did you try this?
>
>   System.setOut(null);
>
> I believe I did that a long time ago, and it seemed to work
> (silenced the output, didn't throw null pointer exceptions).

The main problem I would have with that is that the docs do not say
anything about it - I just looked up System.setOut(PrintStream) for
Java 1.5.0 again.

A lesser problem is that when I tried it, it sure threw the exception:

[510] cat Roska.java
class Roska { public static void main(String [] args) {
   System.setOut(null);
   System.out.println("Hello?");
}}
[511] javac Roska.java
[512] java -cp . Roska
Exception in thread "main" java.lang.NullPointerException
       at Roska.main(Roska.java:3)

This is from version 1.4.0; they haven't updated for a while.

Maybe you did it in a program that does not use System.out? The
exception is not from setOut() but from println() after that.
Daniel Dyer - 27 Nov 2006 16:30 GMT
> The main problem I would have with that is that the docs do not say
> anything about it - I just looked up System.setOut(PrintStream) for
[quoted text clipped - 16 lines]
> Maybe you did it in a program that does not use System.out? The
> exception is not from setOut() but from println() after that.

It's not surprising, you are dereferencing a null.  There are three  
alternatives that come to mind:

1).  Don't use System.out, but I guess if that were viable you wouldn't be  
asking.

2).  Redirect stdout to /dev/null outside of the VM:

    java MyClass > /dev/null

3).  Create a sub-class of PrintStream that discards everything and use  
setOut to point to that.

Dan.

Signature

Daniel Dyer
http://www.dandyer.co.uk

Jussi Piitulainen - 27 Nov 2006 16:51 GMT
...
>> A lesser problem is that when I tried it, it sure threw the exception:
...
>> class Roska { public static void main(String [] args) {
>>     System.setOut(null);
>>     System.out.println("Hello?");
>> }}
...
> It's not surprising, you are dereferencing a null.

It's not just System.out = null; and while the docs say setOut
'reassigns the "standard" output stream', they don't say that it
simply reassigns it to its argument as is.

> There are three alternatives that come to mind:
>
> 1).  Don't use System.out, but I guess if that were viable you
> wouldn't be  asking.

(The one asking was somebody else. I was just curious about a claim
that System.setOut(null) would, maybe, work, so I tried.)

> 2).  Redirect stdout to /dev/null outside of the VM:
>
>     java MyClass > /dev/null
>
> 3).  Create a sub-class of PrintStream that discards everything and
> use setOut to point to that.

The winning way so far appears to be to subclass OutputStream so, and
pass the resulting OutputStream to the appropriate PrintStream
constructor. It's in another branch of this thread already, after I
had suggested your number 3.
Daniel Dyer - 27 Nov 2006 16:56 GMT
>> There are three alternatives that come to mind:
>>
[quoted text clipped - 15 lines]
> constructor. It's in another branch of this thread already, after I
> had suggested your number 3.

My apologies, I seem to have missed the other branch of this thread,  
didn't mean to be redundant.

Dan.

Signature

Daniel Dyer
http://www.dandyer.co.uk

Oliver Wong - 27 Nov 2006 17:04 GMT
>>> is there a garbage file in Java like /dev/null in UNIX?
>>> I want temporarily throw away System.out outputs.
[quoted text clipped - 21 lines]
> Exception in thread "main" java.lang.NullPointerException
>        at Roska.main(Roska.java:3)

   I tried it again in 1.6, and it threw the NPE also. I guess I
misremembered what I had done.

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.