Reassign standard Output Stream.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #1

    Reassign standard Output Stream.

    Today I came across a thing, is it possible to change the Standard Output Stream?
    Then I immediately went for assign the System.out variable.

    [code=java]
    System.out = new PrintStream(new File("some_file _name"));
    [/code]

    Then I got a message, the final variable not to be updated.

    Then I came to know that I have to take help from API,
    [code=java]System.setOut(n ew PrintStream(new File("")))[/code]

    So how Java updates the System.out as it's final.
    Please help me to know it.

    Kind regards,
    Dmjpro.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by dmjpro
    Today I came across a thing, is it possible to change the Standard Output Stream?
    Then I immediately went for assign the System.out variable.

    [code=java]
    System.out = new PrintStream(new File("some_file _name"));
    [/code]

    Then I got a message, the final variable not to be updated.

    Then I came to know that I have to take help from API,
    [code=java]System.setOut(n ew PrintStream(new File("")))[/code]

    So how Java updates the System.out as it's final.
    Please help me to know it.

    Kind regards,
    Dmjpro.
    Java does not change the System.out at all.
    What makes you think that it changes it?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by r035198x
      Java does not change the System.out at all.
      What makes you think that it changes it?
      Duh, that System class is full of native methods and stuff; it even uses a
      sun.misc.Shared Secrets class for several purposes; I don't trust that class
      one single bit ;-)

      kind regards,

      Jos

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by JosAH
        Duh, that System class is full of native methods and stuff; it even uses a
        sun.misc.Shared Secrets class for several purposes; I don't trust that class
        one single bit ;-)

        kind regards,

        Jos
        Version 6? My 1.5.0_03 uses no such secrets.
        I

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by r035198x
          Version 6? My 1.5.0_03 uses no such secrets.
          I
          Yup; check your src.zip that comes with the jdk; that class is full of it.

          kind regards,

          Jos

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by JosAH
            Yup; check your src.zip that comes with the jdk; that class is full of it.

            kind regards,

            Jos
            I'm sticking with 1.5 for my current project.
            I'd like to find time and look to see what those guys were doing with a secrets class though.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by r035198x
              I'm sticking with 1.5 for my current project.
              I'd like to find time and look to see what those guys were doing with a secrets class though.
              They don't want us to know that, it's a class in the sun.misc package and they
              don't give source for any class in those packages; they're 'SharedSecrets' .

              kind regards,

              Jos

              Comment

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                Originally posted by JosAH
                They don't want us to know that, it's a class in the sun.misc package and they
                don't give source for any class in those packages; they're 'SharedSecrets' .

                kind regards,

                Jos
                Reminds me of this issue, but I'd better keep that to myself... ;-)

                Greetings,
                Nepomuk

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by nepomuk
                  Reminds me of this issue, but I'd better keep that to myself... ;-)

                  Greetings,
                  Nepomuk
                  It doesn't matter much: real men (like me) read octal or hex dumps anyway ;-)

                  kind regards,

                  Jos (aka 004a006f0073 (big endian unicode) ;-)

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by JosAH
                    It doesn't matter much: real men (like me) read octal or hex dumps anyway ;-)

                    kind regards,

                    Jos (aka 004a006f0073 (big endian unicode) ;-)
                    Oh yeah, there is always a legal way of reconstructing someone's source code even though the source code is supposed to be "private".

                    Comment

                    • dmjpro
                      Top Contributor
                      • Jan 2007
                      • 2476

                      #11
                      Originally posted by r035198x
                      Java does not change the System.out at all.
                      What makes you think that it changes it?
                      Then how does Java Manage it?
                      Please help !

                      Kind regards,
                      Dmjpro.

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by dmjpro
                        Then how does Java Manage it?
                        Please help !

                        Kind regards,
                        Dmjpro.
                        Manage what? I'm not really getting your question here. For printing to the console the methods print and println are used. The variable out itself is not assigned to any other value.

                        Comment

                        • Nepomuk
                          Recognized Expert Specialist
                          • Aug 2007
                          • 3111

                          #13
                          Originally posted by dmjpro
                          Then how does Java Manage it?
                          Please help !

                          Kind regards,
                          Dmjpro.
                          I think you misunderstand, what happens. Java can't set the systems standard in- and output, but it can set it's own standard in- and output. (Which is exactly what it does with that method.)

                          Greetings,
                          Nepomuk

                          Comment

                          • dmjpro
                            Top Contributor
                            • Jan 2007
                            • 2476

                            #14
                            Originally posted by r035198x
                            Manage what? I'm not really getting your question here. For printing to the console the methods print and println are used. The variable out itself is not assigned to any other value.
                            Actually System.out is assigned with that that's why the print or println goes there.
                            Now if I changed it to file then how Java reassign the System.out or Java does something else.
                            That's my Question.

                            Kind regards,
                            Dmjpro.

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by dmjpro
                              Actually System.out is assigned with that that's why the print or println goes there.
                              Now if I changed it to file then how Java reassign the System.out or Java does something else.
                              That's my Question.

                              Kind regards,
                              Dmjpro.
                              When you redirect the output you don't change the out variable itself. It is final and cannot be reassigned.
                              You just provide the runtime with a new PrintStream object to use when printing out.

                              Comment

                              Working...