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
You can do something like:
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: