1 package hahaa; 2 3 import java.io.FileNotFoundException; 4 import java.io.PrintStream; 5 6 public class ha { 7 public static void main(String[] args) { 8 try { 9 PrintStream out = System.out; //保存原输出流10 PrintStream ps = new PrintStream("./log.txt");//创建文件输出流 11 12 System.setOut(ps); //设置使用新的文件输出流13 int age = 11; 14 System.out.println("年龄变量成功定义,初始值为11"); 15 String sex = "女"; 16 System.out.println("年龄变量成功定义,初始值为女"); 17 // 整合2个变量 18 String info = "这是个" + sex + "孩子,应该有" + age + "岁了";19 System.out.println("整合两个变量为info字符,结果是:"+info);20 System.setOut(out); //又回到控制台21 System.out.println("程序运行完毕,请查看日志"); 22 } catch (FileNotFoundException e) { 23 e.printStackTrace(); 24 } 25 }26 }