try {
//保存原输出流
PrintStream out = System.out;
//创建文件输出流
PrintStream ps = new PrintStream("./log.txt");
//设置使用新的输出流
System.setOut(ps);
//得到要写入的数据这里用自己定义的代替
int age = 18;
System.out.println("age variable defind success ,init value is 18 !");
//拼接要写入日志的内容
String info = "this people is about "+age+" year !";
System.out.println("the result is :"+info);
//恢复原有输出流
System.setOut(out);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}