File /dev/null in Java ?

  • Thread starter =?iso-8859-1?q?J=FCrgen_Gerstacker?=
  • Start date
?

=?iso-8859-1?q?J=FCrgen_Gerstacker?=

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
 
J

Jussi Piitulainen

Jürgen Gerstacker said:
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?

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.
 
F

Furious George

Jussi said:
Jürgen Gerstacker said:
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?

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) {}
}

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 ) { } } ) ) ;
 
J

Jussi Piitulainen

Furious said:
Jussi said:
Jürgen Gerstacker said:
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:

public class NullPrintStream extends PrintStream {
void write(byte[] buf, int off, int len) {}
void write(int b) {}
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 ) { } } ) ) ;

Nice. That may be the solution.
 
?

=?iso-8859-1?q?J=FCrgen_Gerstacker?=

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

This works greatly. Thank you!
Juergen
 
O

Oliver Wong

Jürgen Gerstacker said:
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?

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
 
J

Jussi Piitulainen

Oliver said:
....
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.
 
D

Daniel Dyer

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.

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.
 
J

Jussi Piitulainen

Daniel said:
Jussi Piitulainen wrote: ....
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.
 
D

Daniel Dyer

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


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.

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

Dan.
 
O

Oliver Wong

Jussi Piitulainen said:
Oliver said:
...
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)

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

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top