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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© liguangcaiup 中级黑马   /  2013-7-1 21:27  /  1457 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 liguangcaiup 于 2013-7-2 07:33 编辑
  1. public class IO {
  2.         public static void main(String[] args) throws IOException {
  3.                 try {
  4.                         Properties properties = System.getProperties();
  5.                         properties.list(new PrintStream("a.txt"));        //记录系统信息
  6.                         int[] A = new int[2];
  7.                         int a = A[3];                                  //产生越界异常
  8.                 } catch (Exception e) {

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

评分

参与人数 1技术分 +1 收起 理由
神之梦 + 1 神马都是浮云

查看全部评分

2 个回复

倒序浏览
流使用起来就是各种流对象包装来包装去的
PrintStream 的构造方法可以接收一个OutputStream对象,而FileOutputStream 的构造方法又有一个可以指定在指定文件中添加数据的,所以代码按下面的改就可以做到不覆盖原来的信息了
  1. package com.itheima.io.print;

  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.PrintStream;
  5. import java.util.Properties;

  6. public class PrintStreamTest {
  7.     public static void main(String[] args) throws IOException {
  8.             try {
  9.                     Properties properties = System.getProperties();
  10.                     properties.list(new PrintStream(new FileOutputStream("a.txt", true)));        //记录系统信息  并且在记录时不覆盖原来的
  11.                     int[] A = new int[2];
  12.                     int a = A[3];                                  //产生越界异常
  13.             } catch (Exception e) {

  14.                     e.printStackTrace(new PrintStream(new FileOutputStream("a.txt", true)));//记录异常信息
  15.             }
  16.     }
  17. }
复制代码

评分

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

查看全部评分

回复 使用道具 举报
楼上代码编译可以通过,运行不行.
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马