JAVA之IO技术-将java程序的异常信息保存在文件中

2021/6/3 20:22:38

本文主要是介绍JAVA之IO技术-将java程序的异常信息保存在文件中,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

package ioTest.io2;

import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Properties;
/*
 * 将应用程序的异常信息输出到指定的log文件中
 */
public class ExceptionToFile {

	public static void main(String[] args) throws FileNotFoundException  {
		int[] a=new int[3];
		try {
			System.out.println(a[3]);
		} catch (Exception e) {
			e.printStackTrace();//这个函数也可以传入一个OutputStream类型的对象参数
			//改变一下标准的输出设备
			System.setOut(new PrintStream("exception.log"));
			e.printStackTrace(System.out);
		}	
		//系统信息的持久化
			outputSystemInfo();
	}
	//将系统信息持久化的方法
	public static void outputSystemInfo() {
		Properties pro=System.getProperties();
		try {
			System.setOut(new PrintStream("properties.log"));
		} catch (FileNotFoundException e) {
			System.out.println("创建文件失败");
		}
		pro.list(System.out);
	}
}



这篇关于JAVA之IO技术-将java程序的异常信息保存在文件中的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程