0

我正在尝试使用 FileWriter 创建 4 个索引文件。但是,此代码会引发并发修改异常。我搜索了几个论坛,发现使用 finally 作为解决方案,这很有效。请告诉我错误在哪里。

    public synchronized void writeToDisk() throws IndexerException {
    // TODO Implement this method
    BufferedWriter bw = null;
    try {
        //System.out.println("entering writeToDisk");
        File file = new File("/Users/workspace/master/tmp//Index.txt");
        if(IndexAuthor==-1){
        file = new File("/Users/workspace/master/tmp/Author.txt");

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

        if(IndexTerm==-1){
            file = new File("/Users/workspace/master/tmp/Term.txt");

            if (!file.exists()) {
                file.createNewFile();
            }
            }
        if(IndexCategory==-1){
            file = new File("/Users/workspace/master/tmp/Category.txt");

            if (!file.exists()) {
                file.createNewFile();
            }
            }
        if(IndexLink==-1){
         file = new File("/Users/workspace/master/tmp/Link.txt");

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

        FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
        bw = new BufferedWriter(fw);
        bw.write(PostingsList.toString()); 
        //bw.close();
        //System.out.println("Done");
    } catch (IOException e) {
        //do nothing
    }
    finally
    {
        try {
            if (bw == null)
            bw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
4

0 回答 0