Redirect output from console to a file

N

Nishi Bhonsle

Hi:

How can I redirect some output on my console to a file that is opened in append mode for writing?

Thanks, Nishi.
 
T

Thomas Weidenfeller

Nishi said:
Hi:

How can I redirect some output on my console to a file that is opened in append mode for writing?

If you want to do it from the outside of the program, it entirely
depends on your operating system. Windows command line interpreter and
Unix shells support I/O redirection via '>' (new file), and '>>' (if you
want to append to an existing one). For details, ask in a group related
to your operating system.

If you want to do it from withing Java, you can replace the PrintStream
used by System.out, see the System class API documentation.

/Thomas
 
T

Tony Morris

"replace the PrintStream used by System.out"

A useless (but with an interesting answer) trivia question for you:
How can there be a setErr, setOut and setIn method when System.err,
System.out, and System.in are declared final ?

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
J

Joona I Palaste

Tony Morris said:
"replace the PrintStream used by System.out"
A useless (but with an interesting answer) trivia question for you:
How can there be a setErr, setOut and setIn method when System.err,
System.out, and System.in are declared final ?

Those streams might be wrapper streams around some private streams that
do the "real" work. The setErr, setOut and setIn methods change those
private streams but not System.err, System.out and System.in. That
would be my guess.
 
J

Jon Skeet

Joona I Palaste said:
Those streams might be wrapper streams around some private streams that
do the "real" work. The setErr, setOut and setIn methods change those
private streams but not System.err, System.out and System.in. That
would be my guess.

Unfortunately that's not how it works - basically they rely on private
native methods to change the values of System.out, System.err and
System.in.

Quite a good example of where something should actually have been a
property rather than a public field, really...
 
T

Tony Morris

"basically they rely on private
native methods to change the values of System.out, System.err and
System.in."

Correct.
Discussion with the language designers reveal that this was an oversight at
the time.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top