Redirect output from console to a file

Discussion in 'Java' started by Nishi Bhonsle, Jan 15, 2004.

  1. Hi:

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

    Thanks, Nishi.
    Nishi Bhonsle, Jan 15, 2004
    #1
    1. Advertising

  2. Nishi Bhonsle

    Tony Morris Guest

    Tony Morris, Jan 15, 2004
    #2
    1. Advertising

  3. Nishi Bhonsle wrote:
    > 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
    Thomas Weidenfeller, Jan 15, 2004
    #3
  4. Nishi Bhonsle

    Tony Morris Guest

    "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)


    "Thomas Weidenfeller" <> wrote in message
    news:bu5g8a$dde$...
    > Nishi Bhonsle wrote:
    > > 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
    >
    Tony Morris, Jan 15, 2004
    #4
  5. Tony Morris <> scribbled the following:
    > "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.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "All that flower power is no match for my glower power!"
    - Montgomery Burns
    Joona I Palaste, Jan 16, 2004
    #5
  6. Nishi Bhonsle

    Jon Skeet Guest

    Joona I Palaste <> wrote:
    > Tony Morris <> scribbled the following:
    > > "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.


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

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
    Jon Skeet, Jan 16, 2004
    #6
  7. Nishi Bhonsle

    Tony Morris Guest

    "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)


    "Jon Skeet" <> wrote in message
    news:MPG.1a71e0ab48635251989719@10.1.1.42...
    > Joona I Palaste <> wrote:
    > > Tony Morris <> scribbled the following:
    > > > "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.

    >
    > 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...
    >
    > --
    > Jon Skeet - <>
    > http://www.pobox.com/~skeet
    > If replying to the group, please do not mail me too
    Tony Morris, Jan 18, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. 28tommy
    Replies:
    2
    Views:
    11,231
    28tommy
    Dec 29, 2005
  2. =?Utf-8?B?QUFPTSBUaW0=?=

    How can I redirect Console.Writeline to Response.Output?

    =?Utf-8?B?QUFPTSBUaW0=?=, Mar 21, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    635
    =?Utf-8?B?QUFPTSBUaW0=?=
    Mar 21, 2007
  3. Jamaica R.
    Replies:
    11
    Views:
    1,468
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Jun 9, 2007
  4. Replies:
    5
    Views:
    3,409
    James Kanze
    Mar 4, 2008
  5. AAOM Tim
    Replies:
    2
    Views:
    157
    AAOM Tim
    Mar 21, 2007
Loading...

Share This Page