Results 1 to 8 of 8
Thread: File handling-streams
- 03-24-2011, 03:04 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 61
- Rep Power
- 0
File handling-streams
package streams;
import java.io.FileNotFoundException;
import java.io.PrintStream;
public class Mgr18Extend
{
public static void main(String[] args) throws FileNotFoundException}
{
PrintStream p1 = new PrintStream("check.txt");}
PrintStream p2 = System.out;
System.setOut(p1);
p1.println(1);
p2.println(2);
System.out.println(3);
System.setOut(p2);
p1.println(4);
p2.println(5);
System.out.println(6);
System.setOut(p1);
p1.println(7);
p2.println(8);
System.out.println(9);
output of check.txt:-
1
3
4
7
9
output of console:-
2
5
6
8
here p1 is pointing to file system(check.txt) where as p2 is pointing to console
but though i am printing 3 as System.out.println(3); it is not printing in the console,it is printing in the file(check.txt) y it so.?
- 03-24-2011, 03:56 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 25
What do you think this line is doing:
Java Code:System.setOut(p1);
- 03-24-2011, 04:52 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 61
- Rep Power
- 0
- 03-24-2011, 04:56 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 25
Yes.
And what is p2?
You didn't answer the question, though...what is setOut() doing?
- 03-24-2011, 05:19 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 61
- Rep Power
- 0
- 03-24-2011, 05:26 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 25
So...when you System.setOut(p2) what is happening?
- 03-24-2011, 05:46 PM #7
Member
- Join Date
- Feb 2011
- Posts
- 61
- Rep Power
- 0
- 03-25-2011, 08:56 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 25
You shouldn't guess.
You should look at the API.
Reassigns the "standard" output stream.
Similar Threads
-
File handling
By Dayanand in forum New To JavaReplies: 3Last Post: 03-23-2011, 09:28 AM -
File handling
By Claude2005 in forum New To JavaReplies: 17Last Post: 09-21-2010, 11:49 AM -
Regarding File Handling
By ravjot28 in forum New To JavaReplies: 1Last Post: 01-20-2010, 09:15 PM -
Backslash, File directories, and streams
By rp181 in forum Advanced JavaReplies: 4Last Post: 10-15-2009, 09:54 AM -
Runtime.exec(), handling input and output streams
By crookshank in forum New To JavaReplies: 0Last Post: 06-05-2008, 02:41 PM
Bookmarks