package test;
import java.io.FileNotFoundException;
import java.io.PrintStream;
/**
* Created by lenovo on 2016/6/2.
*/
public class printstream {
public static void main(String[] args) throws FileNotFoundException {
PrintStream out=System.out; //保存后台的输出
PrintStream ps=new PrintStream("./log.txt"); //创建新的输出流 即下面system.out的内容会写到txt中 而不是后台
System.setOut(ps);
int age=18;
System.out.println("Age has been reset successfully! Initialization is "+age);
String sex="Female";
System.out.println("Sex has been reset successfully! Initialization is "+sex);
System.setOut(out); //恢复原有的输出流 即SEE输出到后台 而不是txt文件中
System.out.println("SEE");
}
}
网友评论