Results 1 to 8 of 8
  1. #1
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default 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.?

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,944
    Rep Power
    19

    Default

    What do you think this line is doing:
    Java Code:
    System.setOut(p1);
    ?

  3. #3
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    What do you think this line is doing:
    Java Code:
    System.setOut(p1);
    ?
    System.setOut(p2);
    System.out.println(6);


    but 6 is printing in the console,though System.setOut(p2); statement is there

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,944
    Rep Power
    19

    Default

    Yes.
    And what is p2?

    You didn't answer the question, though...what is setOut() doing?

  5. #5
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Yes.
    And what is p2?

    You didn't answer the question, though...what is setOut() doing?
    p2 is pointing to console which is a Stream variable.....

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,944
    Rep Power
    19

    Default

    So...when you System.setOut(p2) what is happening?

  7. #7
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    So...when you System.setOut(p2) what is happening?
    i think p2 will be pointing to console...........?
    Last edited by Dayanand; 03-24-2011 at 05:54 PM.

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,944
    Rep Power
    19

    Default

    You shouldn't guess.
    You should look at the API.
    Reassigns the "standard" output stream.
    It has no effect on p2. It is setting System.out.

Similar Threads

  1. File handling
    By Dayanand in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 09:28 AM
  2. File handling
    By Claude2005 in forum New To Java
    Replies: 17
    Last Post: 09-21-2010, 11:49 AM
  3. Regarding File Handling
    By ravjot28 in forum New To Java
    Replies: 1
    Last Post: 01-20-2010, 09:15 PM
  4. Backslash, File directories, and streams
    By rp181 in forum Advanced Java
    Replies: 4
    Last Post: 10-15-2009, 09:54 AM
  5. Runtime.exec(), handling input and output streams
    By crookshank in forum New To Java
    Replies: 0
    Last Post: 06-05-2008, 02:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •