用附加而不是覆盖的Java文件写入?

时间:2014-11-10 14:06:33

标签: java filewriter

我有这个代码创建一个文件并保存用户输入,但它会保持覆盖,我希望它保存用户提供的每个条目。我怎么能这样做?

    File file = new File("info.txt");
    BufferedWriter output = null;
    try {
        output = new BufferedWriter(new FileWriter(file));
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        output.write("Users pick:  " + myint+ "\t");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        output.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //file writer

1 个答案:

答案 0 :(得分:3)

更改

output = new BufferedWriter(new FileWriter(file));

output = new BufferedWriter(new FileWriter(file,true));

以附加模式打开文件。