BufferedWriter如何不写已保存的文本?

时间:2013-02-24 14:48:20

标签: java file

嗨大家好奇,如果你能帮助我,我正在尝试创建一个日志文件,它保存用户选择的所有变量,到目前为止我有:

try{
         String content = (" User : " + username + "\n Print job to be printed at : " + (dateFormat.format(cal.getTime())) +"\n Copies selected : " + p +"\n Papersize selected : " + paperSize + "\n Duplex selected : " +duplex + "\n Oreination Selected : " +orienation + "\n Printer Selected : " +printer);

         //File file = new File(log);


         FileWriter fw = new FileWriter(log);
         BufferedWriter bw = new BufferedWriter(fw);
         bw.write(content);
         bw.close();

         System.out.println("Logged the print job");

         } catch (IOException e) {
         e.printStackTrace();
         }

这完全正常,但是我希望下次调用它来将数据添加到文件中已有的数据,而不是覆盖它,这可能吗?因为它比每次创建一个新文件要好得多,

由于

1 个答案:

答案 0 :(得分:3)

更改构造函数以使用boolean append并发送true

FileWriter fw = new FileWriter(log, true);

来自:FileWriter(File file, boolean append)