aspose file tools*
The moose likes Beginning Java and the fly likes how this setOut method works....... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how this setOut method works......." Watch "how this setOut method works......." New topic
Author

how this setOut method works.......

Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45


hi in the above code i am using setOut method to direct some of the inputs to "test.txt" file and some to the console. According to me the output should be
console :- test.txt:-
1 4
2 8
3 10 <--> 9 (actual output)
5 11
6
7
9 <--> 10 (actual output)
12
13
14
15

but in the actual output is 9 and 10 are replaced with each other. so please explain me how is it working???
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 375
Your program seems to be working correctly to me.

When you do System.out.println(9);
you have already done System.setOut(p1); so it should print to the text file.

When you do p.println(10);
you have done System.setOut(p); so it will print to the console..

Isn't it?
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 690
    
    1
If you'd put comments next to each line to show how the state changes and what the expected output is to be it might help you understand what is happening.
Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45

According to me the output should be :
in console :-
1
2
3
5
6
7
9
12
13
14
15

and in text file:-
4
8
10
11

but the actual output is
console:-
1
2
3
5
6
7
10
12
13
14
15

and text file :-
4
8
9
11
so thats why i am not getting the proper concept of setOut() method, i also checked the api but i couldn't get proper answer.
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 375
Puneet, can you reason out why the out put should be like what you think?
Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45

since initially p is ref to System.out so 2 and 3 will go to console, than p1 is referred to test.txt so 4 will go to test, similarly p is still referring to System.out so again 5 and 6 will go to console. now setOut is taking argument as p1 but i didn't understand what it means. Does that means like set the output of reference of type System to the destination which is referred by p1. if it is so than ok it will produce 9 in the test.txt and 10 in the console. but if it doesn't mean that, than please tell me the concept. Actually please tell me what exactly is this statement doing:-
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 375
Puneet,
The API doc
says..

Reassigns the "standard" output stream.
Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45

ok thanks i got it now.
thanks again.....
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how this setOut method works.......