Q. How to redirect System.out?
A: Use System.setOut()
Sample code:
import java.io.*;
class Redirection {
public static void main(String args[]) throws IOException {
PrintStream pos =new PrintStream(new FileOutputStream("applic.log"));
PrintStream oldstream=System.out;
System.out.println("Message 1 appears on console");
System.setOut(pos);
System.out.println("Message 2 appears on file");
System.out.println("Message 3 appears on file");
System.out.println("Message 4 appears on file");
System.setOut(oldstream);
System.out.println("Message 5 appears on console");
System.out.println("Message 6 appears on console");
}
}
No comments:
Post a Comment