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. -
System.out = new PrintStream(new File("some_file_name"));
-
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, - 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.
27 3856
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. -
System.out = new PrintStream(new File("some_file_name"));
-
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, - 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?
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
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
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
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.
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
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
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) ;-)
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".
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.
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.
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
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.
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.
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 - 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.
That can be done using - 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: -
public class System {
-
private static PrintWriter ps = new PrintStream(...); // Whatever the standard output is
-
public static void setOut(PrintWriter nps)
-
{
-
ps = nps;
-
}
-
}
-
Greetings,
Nepomuk
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: -
public class System {
-
private static PrintWriter ps = new PrintStream(...); // Whatever the standard output is
-
public static void setOut(PrintWriter nps)
-
{
-
ps = nps;
-
}
-
}
-
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.
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
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): -
public class System {
-
private static PrintStream ps = new PrintStream(...); // standard OutputStream
-
public static setOut(Printstream nps)
-
{
-
ps = nps;
-
}
-
public static void print(char c)
-
{
-
ps.write(c);
-
}
-
public static void print(String s)
-
{
-
for(int i=0; i<s.length(); i++)
-
{
-
print(s.charAt(i));
-
}
-
}
-
public static void println(String s)
-
{
-
print(s + '\n');
-
}
-
// ...
-
}
-
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
It doesn't. It doesn't even care.
It could look like this (simplified): -
public class System {
-
private static PrintStream ps = new PrintStream(...); // standard OutputStream
-
public static setOut(Printstream nps)
-
{
-
ps = nps;
-
}
-
public static void print(char c)
-
{
-
ps.write(c);
-
}
-
public static void print(String s)
-
{
-
for(int i=0; i<s.length(); i++)
-
{
-
print(s.charAt(i));
-
}
-
}
-
public static void println(String s)
-
{
-
print(s + '\n');
-
}
-
// ...
-
}
-
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.
It doesn't. It doesn't even care.
It could look like this (simplified): -
public class System {
-
private static PrintStream ps = new PrintStream(...); // standard OutputStream
-
public static setOut(Printstream nps)
-
{
-
ps = nps;
-
}
-
public static void print(char c)
-
{
-
ps.write(c);
-
}
-
public static void print(String s)
-
{
-
for(int i=0; i<s.length(); i++)
-
{
-
print(s.charAt(i));
-
}
-
}
-
public static void println(String s)
-
{
-
print(s + '\n');
-
}
-
// ...
-
}
-
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
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.
...
ps = nps;
...
That wouldn't be the case because ps is actually final.
That wouldn't be the case because ps is actually final.
And it isn't called 'ps', its name is 'out' ;-)
kind regards,
Jos
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!) -
import java.io.*;
-
-
public class Sys {
-
private static NewPrintStream pStream = new NewPrintStream(System.out);
-
// Of course, this won't be with "System.out", but that way you can see the output - they must get an OutputStream from somewhere
-
public static final PrintStream out = pStream;
-
public static void setOut(PrintStream nps)
-
{
-
pStream.setOut(nps);
-
}
-
}
-
class NewPrintStream extends PrintStream {
-
public NewPrintStream(OutputStream os)
-
{
-
super(os);
-
}
-
public void setOut(PrintStream ps)
-
{
-
this.out = ps;
-
}
-
}
-
When testing it with this -
import java.io.*;
-
-
public class Test {
-
public static void main(String[] args)
-
{
-
Sys.out.println("Hi");
-
try
-
{
-
Sys.setOut(new PrintStream(new FileOutputStream(new File("hello.txt"))));
-
}
-
catch(FileNotFoundException fnfe)
-
{
-
System.err.println("Didn't work.");
-
}
-
Sys.out.println("Hi");
-
}
-
}
-
It prints the first output of "Hi" to the standard output and the second one to a file called "hello.txt".
Greetings,
Nepomuk
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!) -
import java.io.*;
-
-
public class Sys {
-
private static NewPrintStream pStream = new NewPrintStream(System.out);
-
// Of course, this won't be with "System.out", but that way you can see the output - they must get an OutputStream from somewhere
-
public static final PrintStream out = pStream;
-
public static void setOut(PrintStream nps)
-
{
-
pStream.setOut(nps);
-
}
-
}
-
class NewPrintStream extends PrintStream {
-
public NewPrintStream(OutputStream os)
-
{
-
super(os);
-
}
-
public void setOut(PrintStream ps)
-
{
-
this.out = ps;
-
}
-
}
-
When testing it with this -
import java.io.*;
-
-
public class Test {
-
public static void main(String[] args)
-
{
-
Sys.out.println("Hi");
-
try
-
{
-
Sys.setOut(new PrintStream(new FileOutputStream(new File("hello.txt"))));
-
}
-
catch(FileNotFoundException fnfe)
-
{
-
System.err.println("Didn't work.");
-
}
-
Sys.out.println("Hi");
-
}
-
}
-
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?
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
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
|
3 posts
views
Thread by gouki |
last post: by
|
7 posts
views
Thread by Steven T. Hatton |
last post: by
|
1 post
views
Thread by Googler |
last post: by
|
16 posts
views
Thread by ben beroukhim |
last post: by
|
9 posts
views
Thread by kernelxu |
last post: by
|
6 posts
views
Thread by Seenivasan Palaniappan |
last post: by
| |
1 post
views
Thread by terminatorul |
last post: by
| | | | | | | | | | |