469,336 Members | 5,570 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

Reassign standard Output Stream.

dmjpro
2,476 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. System.out = new PrintStream(new File("some_file_name"));
  2.  
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,
Expand|Select|Wrap|Line Numbers
  1. System.setOut(new PrintStream(new File("")))
So how Java updates the System.out as it's final.
Please help me to know it.

Kind regards,
Dmjpro.
Sep 26 '07 #1
27 3765
r035198x
13,262 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. System.out = new PrintStream(new File("some_file_name"));
  2.  
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,
Expand|Select|Wrap|Line Numbers
  1. System.setOut(new PrintStream(new File("")))
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?
Sep 26 '07 #2
JosAH
11,448 Expert 8TB
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.SharedSecrets class for several purposes; I don't trust that class
one single bit ;-)

kind regards,

Jos
Sep 26 '07 #3
r035198x
13,262 8TB
Duh, that System class is full of native methods and stuff; it even uses a
sun.misc.SharedSecrets 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
Sep 26 '07 #4
JosAH
11,448 Expert 8TB
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
Sep 26 '07 #5
r035198x
13,262 8TB
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.
Sep 26 '07 #6
JosAH
11,448 Expert 8TB
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
Sep 26 '07 #7
Nepomuk
3,112 Expert 2GB
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
Sep 26 '07 #8
JosAH
11,448 Expert 8TB
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) ;-)
Sep 26 '07 #9
r035198x
13,262 8TB
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".
Sep 26 '07 #10
dmjpro
2,476 2GB
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.
Sep 27 '07 #11
r035198x
13,262 8TB
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.
Sep 27 '07 #12
Nepomuk
3,112 Expert 2GB
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
Sep 27 '07 #13
dmjpro
2,476 2GB
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.
Sep 27 '07 #14
r035198x
13,262 8TB
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.
Sep 27 '07 #15
dmjpro
2,476 2GB
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.
That can be done using
Expand|Select|Wrap|Line Numbers
  1. System.setOut(new PrintStream(..))
But how this function redirects the O/P?
When System.out.println("") is called then System.out is still standard O/P, in spite of that how the O/P redirects to new one?
Please help !

Kind regards,
Dmjpro.
Sep 27 '07 #16
Nepomuk
3,112 Expert 2GB
That can be done using
Expand|Select|Wrap|Line Numbers
  1. System.setOut(new PrintStream(..))
But how this function redirects the O/P?
When System.out.println("") is called then System.out is still standard O/P, in spite of that how the O/P redirects to new one?
Please help !

Kind regards,
Dmjpro.
The System class has a PrintWriter from the beginning. You just assign a new PrintWriter in it's position.
It would look something like this:
Expand|Select|Wrap|Line Numbers
  1. public class System {
  2.    private static PrintWriter ps = new PrintStream(...); // Whatever the standard output is
  3.    public static void setOut(PrintWriter nps)
  4.    {
  5.       ps = nps;
  6.    }
  7. }
  8.  
Greetings,
Nepomuk
Sep 27 '07 #17
dmjpro
2,476 2GB
The System class has a PrintWriter from the beginning. You just assign a new PrintWriter in it's position.
It would look something like this:
Expand|Select|Wrap|Line Numbers
  1. public class System {
  2.    private static PrintWriter ps = new PrintStream(...); // Whatever the standard output is
  3.    public static void setOut(PrintWriter nps)
  4.    {
  5.       ps = nps;
  6.    }
  7. }
  8.  
Greetings,
Nepomuk
Ok!
Then how do System.out.println method come to know that standard O/P changed and println method writes to the new one.
Actually println method should have the track of that :-)
Please help !

Kind regards,
Dmjpro.
Sep 27 '07 #18
JosAH
11,448 Expert 8TB
Ok!
Then how do System.out.println method come to know that standard O/P changed and println method writes to the new one.
Actually println method should have the track of that :-)
Please help !

Kind regards,
Dmjpro.
When you install a jdk on your computer. in the jdk's home directory a src.zip
file is stored; it contains all the sources of all the classes in the core library,
including System's source code. Read it and see for yourself how this class
redirects the 'standard' streams.

kind regards,

Jos
Sep 27 '07 #19
Nepomuk
3,112 Expert 2GB
Ok!
Then how do System.out.println method come to know that standard O/P changed and println method writes to the new one.
Actually println method should have the track of that :-)
Please help !

Kind regards,
Dmjpro.
It doesn't. It doesn't even care.

It could look like this (simplified):
Expand|Select|Wrap|Line Numbers
  1. public class System {
  2.    private static PrintStream ps = new PrintStream(...); // standard OutputStream
  3.    public static setOut(Printstream nps)
  4.    {
  5.       ps = nps;
  6.    }
  7.    public static void print(char c)
  8.    {
  9.       ps.write(c);
  10.    }
  11.    public static void print(String s)
  12.    {
  13.       for(int i=0; i<s.length(); i++)
  14.       {
  15.          print(s.charAt(i));
  16.       }
  17.    }
  18.    public static void println(String s)
  19.    {
  20.       print(s + '\n');
  21.    }
  22.    // ...
  23. }
  24.  
so it will just use the PrintStream (ps) that's there. It uses that, when no PrintStream was set manually (it will use the standard PrintStream) and it will use that, after it was changed. It doesn't care, that it's a different Stream, as the name is the same.

Greetings,
Nepomuk
Sep 27 '07 #20
dmjpro
2,476 2GB
It doesn't. It doesn't even care.

It could look like this (simplified):
Expand|Select|Wrap|Line Numbers
  1. public class System {
  2.    private static PrintStream ps = new PrintStream(...); // standard OutputStream
  3.    public static setOut(Printstream nps)
  4.    {
  5.       ps = nps;
  6.    }
  7.    public static void print(char c)
  8.    {
  9.       ps.write(c);
  10.    }
  11.    public static void print(String s)
  12.    {
  13.       for(int i=0; i<s.length(); i++)
  14.       {
  15.          print(s.charAt(i));
  16.       }
  17.    }
  18.    public static void println(String s)
  19.    {
  20.       print(s + '\n');
  21.    }
  22.    // ...
  23. }
  24.  
so it will just use the PrintStream (ps) that's there. It uses that, when no PrintStream was set manually (it will use the standard PrintStream) and it will use that, after it was changed. It doesn't care, that it's a different Stream, as the name is the same.

Greetings,
Nepomuk
println is not the method of System class here :-)

Kind regards,
Dmjpro.
Sep 27 '07 #21
JosAH
11,448 Expert 8TB
It doesn't. It doesn't even care.

It could look like this (simplified):
Expand|Select|Wrap|Line Numbers
  1. public class System {
  2.    private static PrintStream ps = new PrintStream(...); // standard OutputStream
  3.    public static setOut(Printstream nps)
  4.    {
  5.       ps = nps;
  6.    }
  7.    public static void print(char c)
  8.    {
  9.       ps.write(c);
  10.    }
  11.    public static void print(String s)
  12.    {
  13.       for(int i=0; i<s.length(); i++)
  14.       {
  15.          print(s.charAt(i));
  16.       }
  17.    }
  18.    public static void println(String s)
  19.    {
  20.       print(s + '\n');
  21.    }
  22.    // ...
  23. }
  24.  
so it will just use the PrintStream (ps) that's there. It uses that, when no PrintStream was set manually (it will use the standard PrintStream) and it will use that, after it was changed. It doesn't care, that it's a different Stream, as the name is the same.

Greetings,
Nepomuk
But you don't do 'System.print( ... )', you have to use 'System.out.print( ... )'.

kind regards,

Jos
Sep 27 '07 #22
dmjpro
2,476 2GB
But you don't do 'System.print( ... )', you have to use 'System.out.print( ... )'.

kind regards,

Jos
That's right Jos :-)
I will surely read the API Doc :-)

Kind regards,
Dmjpro.
Sep 27 '07 #23
r035198x
13,262 8TB
...
ps = nps;
...
That wouldn't be the case because ps is actually final.
Sep 27 '07 #24
JosAH
11,448 Expert 8TB
That wouldn't be the case because ps is actually final.
And it isn't called 'ps', its name is 'out' ;-)

kind regards,

Jos
Sep 27 '07 #25
Nepomuk
3,112 Expert 2GB
Arg, I hadn't seen it was final... But ok, I've found a solution, which will allow you to reset the standard output. (It may not be the way, it's done within the System class, but it works!)

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class Sys {
  4.     private static NewPrintStream pStream = new NewPrintStream(System.out);
  5.     // Of course, this won't be with "System.out", but that way you can see the output - they must get an OutputStream from somewhere
  6.     public static final PrintStream out = pStream;
  7.     public static void setOut(PrintStream nps)
  8.     {
  9.         pStream.setOut(nps);
  10.     }
  11. }
  12. class NewPrintStream extends PrintStream {
  13.     public NewPrintStream(OutputStream os)
  14.     {
  15.         super(os);
  16.     }    
  17.     public void setOut(PrintStream ps)
  18.     {
  19.         this.out = ps;
  20.     }
  21. }
  22.  
When testing it with this
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class Test {
  4.     public static void main(String[] args)
  5.     {
  6.         Sys.out.println("Hi");
  7.         try
  8.         {
  9.             Sys.setOut(new PrintStream(new FileOutputStream(new File("hello.txt"))));
  10.         }
  11.         catch(FileNotFoundException fnfe)
  12.         {
  13.             System.err.println("Didn't work.");
  14.         }
  15.         Sys.out.println("Hi");
  16.     }
  17. }
  18.  
It prints the first output of "Hi" to the standard output and the second one to a file called "hello.txt".

Greetings,
Nepomuk
Sep 28 '07 #26
r035198x
13,262 8TB
Arg, I hadn't seen it was final... But ok, I've found a solution, which will allow you to reset the standard output. (It may not be the way, it's done within the System class, but it works!)

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class Sys {
  4.     private static NewPrintStream pStream = new NewPrintStream(System.out);
  5.     // Of course, this won't be with "System.out", but that way you can see the output - they must get an OutputStream from somewhere
  6.     public static final PrintStream out = pStream;
  7.     public static void setOut(PrintStream nps)
  8.     {
  9.         pStream.setOut(nps);
  10.     }
  11. }
  12. class NewPrintStream extends PrintStream {
  13.     public NewPrintStream(OutputStream os)
  14.     {
  15.         super(os);
  16.     }    
  17.     public void setOut(PrintStream ps)
  18.     {
  19.         this.out = ps;
  20.     }
  21. }
  22.  
When testing it with this
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class Test {
  4.     public static void main(String[] args)
  5.     {
  6.         Sys.out.println("Hi");
  7.         try
  8.         {
  9.             Sys.setOut(new PrintStream(new FileOutputStream(new File("hello.txt"))));
  10.         }
  11.         catch(FileNotFoundException fnfe)
  12.         {
  13.             System.err.println("Didn't work.");
  14.         }
  15.         Sys.out.println("Hi");
  16.     }
  17. }
  18.  
It prints the first output of "Hi" to the standard output and the second one to a file called "hello.txt".

Greetings,
Nepomuk
Well the System class already has a setOut so there's no need to reinvent the wheel is there?
Sep 28 '07 #27
Nepomuk
3,112 Expert 2GB
Well the System class already has a setOut so there's no need to reinvent the wheel is there?
Reason: Curiosity! ^^

The OP did ask, how it is done and I've posted a possible solution that may be used. That's it.

By the way, if anyone ever wants to change an Object defined as "final", this might be of interest...

Greetings,
Nepomuk
Sep 28 '07 #28

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by Matt Garman | last post: by
7 posts views Thread by Steven T. Hatton | last post: by
1 post views Thread by Googler | last post: by
6 posts views Thread by Seenivasan Palaniappan | last post: by
1 post views Thread by terminatorul | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.