java 以追加的方式写文件

来源:互联网 发布:网络摄监控系统图 编辑:程序博客网 时间:2024/05/18 01:03

这是项目的一个小需求,直接上代码。

  

  private void makeFileRecord(String fileName) {        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");// 设置日期格式        BufferedWriter ous = null;        try {            // 创建文件, System.getProperty("java.io.tmpdir")  获取的是web容器的临时文件夹 我这里是tomcat下的temp文件夹            File file = new File(System.getProperty("java.io.tmpdir") + File.separator + df.format(new Date()) + ".txt");            if(!file.exists()) {                file.createNewFile();            }·           //  FileWriter(file,true)  true  指定是以追加的方式读写。            ous = new BufferedWriter(new FileWriter(file, true));            ous.write(fileName);            ous.newLine();        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                ous.flush();                ous.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }


 


 

1 0