This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 886
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puneet, can you reason out why the out put should be like what you think?
 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puneet,
The API doc
says..

Reassigns the "standard" output stream.
 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks i got it now.
thanks again.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic