A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 宋旭东 中级黑马   /  2013-7-4 22:03  /  1062 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 宋旭东 于 2013-7-4 22:03 编辑


  • public class IO {
  •         public static void main(String[] args) throws IOException {
  •                 try {
  •                         Properties properties = System.getProperties();
  •                         properties.list(new PrintStream("a.txt"));        //记录系统信息
  •                         int[] A = new int[2];
  •                         int a = A[3];                                  //产生越界异常
  •                 } catch (Exception e) {
  •                         e.printStackTrace(new PrintStream("a.txt"));//记录异常信息
  •                 }
  •         }
  • }


e.printStackTrace(new PrintStream("a.txt"));//记录异常信息   会覆盖系统记录的信息
怎么能让异常信息加在系统信息之后呢
如何让PrintStream在已经存在的文件后面加些输出?

1 个回复

倒序浏览
本帖最后由 王洪波 于 2013-7-5 08:55 编辑
  1. public class IO
  2. {
  3.         public static void main(String[] args) throws IOException
  4.         {        
  5.                 PrintStream ps= null;//共用这个流
  6.                 try
  7.                 {
  8.                         Properties properties = System.getProperties();
  9.                         
  10.                         ps = new PrintStream("a.txt");
  11.                         properties.list(ps); // 记录系统信息
  12.                         
  13.                         int[] A = new int[2];
  14.                         int a = A[3]; // 产生越界异常
  15.                 } catch (Exception e)
  16.                 {
  17.                         e.printStackTrace(ps);// 记录异常信息
  18.                 }finally
  19.                 {
  20.                         ps.close();//还要记着关闭流哦!浪费系统资源后果很严重。
  21.                 }
  22.         }
  23. }
复制代码
动了脑筋问题才解决,望版主助唵一分之力啊。:)

评分

参与人数 1技术分 +1 收起 理由
神之梦 + 1 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马