Jannat Arora
Jannat Arora

Reputation: 2989

How to redirect the output of an interactive program to a file?

I have an interactive java program which takes input from user...now i need to redirect whatever output that has been printed on the screen to a file? Is is possible.

From the java docs I got the method "System.setOut(PrintStream ps);" but i am not getting as to how to use this method?

E.g. I have a program as:

public class A{
  int i;
   void func()
   {
      System.out.println("Enter a value:");
      Scanner in1=new Scanner(System.in);
      i= in1.nextInt();
      System.out.println("i="+i);
   }
 }

Now i want to redirect the output given below to a file:

 Enter a value:
 1
 i=1

Upvotes: 1

Views: 447

Answers (3)

dan
dan

Reputation: 13272

You can do something like:

System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("output.txt"))));

To write something to a file there a number of ways, you can take a look at Reading, Writing, and Creating Files tutorial.

In your case, if you would like to print exactly what is on the screen in a file too, even the user input, you can do something like:

void func(){                                                                                             
  try {                                                                                                  
    PrintStream out=new PrintStream(new BufferedOutputStream(new FileOutputStream("output.txt")));       
    System.out.println("Enter a value:");                                                                
    out.println("Enter a value:");                                                                       
    Scanner in1=new Scanner(System.in);                                                                  
    int i= in1.nextInt();                                                                                
    out.println(i);                                                                                      
    System.out.println("i="+i);                                                                          
    out.println("i="+i);                                                                                 
    out.close();                                                                                         
  } catch (FileNotFoundException e) {                                                                    
    System.err.println("An error has occurred "+e.getMessage());                                         
    e.printStackTrace();                                                                                 
  }
}

Upvotes: 2

PermGenError
PermGenError

Reputation: 46408

classes in java.io package are designed for that. i recommend you to have a look at java.io package.

After you edit.

   File file = new File("newFile.txt");
   PrintWriter pw = new PrintWriter(new FileWriter(file));
    pw.println("your input to the file");
    pw.flush();

     pw.close()

Upvotes: 0

Frank
Frank

Reputation: 15641

Here you go:

  // all to the console    
        System.out.println("This goes to the console");
        PrintStream console = System.out; // save the console out for later.
  // now to the file    
        File file = new File("out.txt");
        FileOutputStream fos = new FileOutputStream(file);
        PrintStream ps = new PrintStream(fos);
        System.setOut(ps);
        System.out.println("This goes to the file out.txt");

  // and back to normal
        System.setOut(console);
        System.out.println("This goes back to the console");

Upvotes: 0

Related Questions