package testone.com;
//将向屏幕输出的输出流改为向文件中输出
import java.io.FileNotFoundException;
import java.io.PrintStream;
public class TestOne {
public static void main(String[] args) {
try {
PrintStream out = System.out;
PrintStream ps = new PrintStream("E:/log.txt");
System.setOut(ps);
String name = "wkwk";
System.out.println("姓名变量成功定义,初始值为wkwk");
int age = 18;
System.out.println("年龄变量成功定义,初始值为18");
String info = "姓名为"+name+"的少年,今年"+age+"岁了。";
System.out.println(info);
System.setOut(out);
System.out.println("程序运行完毕,请查看日志文件。");
}catch(FileNotFoundException e) {
e.printStackTrace();
}
}
}
Java中输出流重定向小例子
最新推荐文章于 2022-04-04 09:51:28 发布