黑马程序员技术交流社区

标题: 关于printstream每次都覆盖文件问题。 [打印本页]

作者: H._张_♂    时间: 2014-3-25 18:48
标题: 关于printstream每次都覆盖文件问题。
import java.io.*;
import java.util.*;
import java.text.*;

public class ExceptionInfo
{
    public static void main(String[] args)
    {
        try
        {
            int [] a = new int[2];
            System.out.println(a[2]);
        }catch(Exception e)
        {
        
            PrintStream ps =null;
            try
            {
               
               ps = new PrintStream("D:/e.txt");
            }catch(Exception ex )
            {
                 ex.printStackTrace(ps);
            }
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
            String s = sdf.format(d);
            ps.println(s);
            e.printStackTrace(ps);
        }
    }
}
老师的这个程序为什么每次运行都会覆盖D:/e.txt这个原文件内容,怎么才能设置成不覆盖啊?
作者: yanzhendong    时间: 2014-3-25 19:55
  1. package study;
  2. import java.io.*;

  3. public class test
  4. {
  5.      public static void main(String[] args)throws Exception
  6.      {
  7.   
  8.    
  9.          
  10.                 OutputStream ops=new FileOutputStream("D:/e.txt",true) ;
  11.               PrintStream   ps = new PrintStream(ops);
  12.              ps.println("appended");
  13.                   
  14.          
  15.         
  16.      }
  17. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2