IO流的应用(三)异常打印日志文件中

package com.bjpowernode.demo02;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

/**

  • 打印字节 流
  • PrintStream
  • @author Administrator

*/
public class Test03 {

public static void main(String[] args) throws IOException {
	//1) 
	OutputStream out = new FileOutputStream("d:/log.txt", true); //以追加的方式打开文件
	PrintStream ps = new PrintStream(out);
	
	ps.print("print string ");
	ps.println("heheh");
	
	//2)System类的out成员就是PrintStream类型的
	//默认情况下, System.out代表系统的标准输出设备,即显示器
	System.out.println("hehe");
	//修改System.out的输出方向
	System.setOut(ps);
	System.out.println("再打印的信息,不显示在屏幕上了, 打印到ps流中");
	
	//3) 经常把异常信息打印到日志文件中
	try {
		int xx = 10 / 0 ; 		//会产生算术异常
	} catch (Exception e) {
		// 开发时,一般会把异常打印到屏幕上,方便程序员调试

// e.printStackTrace();
//部署后,会把异常打印到日志文件中,
e.printStackTrace(ps);
}
ps.close();
}

}

猜你喜欢

转载自blog.csdn.net/qq_30347133/article/details/83513400
今日推荐