Java中输出流重定向小例子

版权声明:文章版权归作者所有,请不要随意转载抄袭,情节严重,追究法律责任!! https://blog.csdn.net/Ibelievesunshine/article/details/82871905
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();
		}
	}
}

猜你喜欢

转载自blog.csdn.net/Ibelievesunshine/article/details/82871905