package demo;
import java.io.PrintStream;
import java.io.FileNotFoundException;
public class RedirctOutputStream {
public static void main(String[] args) {
try{
PrintStream out = System.out;
PrintStream outStream = new PrintStream("./log.txt");
/*
/
The name of the file to use as the destination of this print stream.
If the file exists, then it will be truncated to zero size;
otherwise, a new file will be created.
The output will be written to the file and is buffered
*/
System.setOut(outStream);
int age = 1;
System.out.println("confirm age is 1");
String name = "Lucy";
System.out.println("confirm Lucy appear");
//combine variable
System.out.println("confirm "+ name + " is "+ age + " year old");
System.setOut(out);
System.out.println("over !");
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
}
RedirctOutputStream
最新推荐文章于 2018-11-21 20:55:50 发布