创建多个文件

时间:2015-08-29 20:47:43

标签: java file

我有这段代码:

try {

    File file = new File(something+counter+".txt");

    counter++;

    if (!file.exists()) {
        file.createNewFile();
    }

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

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

我需要这样做:当他们按回车键时,将在定义的位置创建新文件(我为此创建了JTextField),但是当他们再次按下它时,文件OVERWRITES。那就是问题所在。我需要他们按Enter键 - 创建新文件:sample1.txt,再次按回车键,创建新文件:sample2.txt

1 个答案:

答案 0 :(得分:1)

现在查看你的代码好了,它看起来很好,但你可能在方法中声明了计数器变量。如果是这样,它将被重置'无论你在每次调用方法时将它设置为什么。

修改

这应该有用。

topics.collection.update({"subfields.subfields_2.keywords.name":"carbon nanotube"}, {$push: {"subfields.subfields_2.keywords.$.queried":time()}})

我得到了

写入file0.txt

写入file1.txt

作为输出,两个文件都有字符串' content'写在他们身上。

EDIT2:

每当用户按Enter键时调用writefile。