Java:将HashMap写入文件

时间:2015-10-14 01:03:18

标签: java file save

我正在开发一个将两个单词保存到HashMap中的程序。我需要能够获取HashMap键和值,并将其作为“key:value”格式写入文件。调用我的save()方法时,应该将HashMap内容写入文件,该文件的名称作为构造函数的参数给出。如果无法保存文件,则该方法返回false;否则返回true。但是,如果文件不存在,它将无法正常工作。它也不保存对现有文件所做的更改。我不太懂得如何读/写文件......谢谢。

package dictionary;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Scanner;


public class MindfulDictionary {

private HashMap<String, String> words;
private File file;

public MindfulDictionary() {
    this.words = new HashMap<String, String>();
}

public MindfulDictionary(String file) {
    this.file = new File(file);
    this.words = new HashMap<String, String>();
}

public boolean load() {
    try {
        Scanner fileReader = new Scanner(this.file);
        while (fileReader.hasNextLine()) {
            String line = fileReader.nextLine();
            String[] parts = line.split(":");   // the line is split at :

            String word = parts[0];
            String trans = parts[1];
            this.add(word, trans);
        }
    } catch (Exception e) {
        System.out.println("nope");
    }
    return true;
}

public boolean save() {
    boolean saved = true;
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(this.file.getName(), true));
        for (String key : this.words.keySet()) {
            writer.write(key + ":" + this.words.get(key) + "\n");
            writer.newLine();
            writer.flush();
            writer.close();
        }
    } catch (Exception e) {

    }
    return saved;
}

public void add(String word, String translation) {
    if ((!this.words.containsKey(word))) {
        this.words.put(word, translation);
    }
}

public String translate(String word) {
    if (this.words.containsKey(word)) {
        return this.words.get(word);
    } else if (this.words.containsValue(word)) {
        for (String key : this.words.keySet()) {
            if (this.words.get(key).equals(word)) {
                return key;
            }
        }
    }
    return null;
}

public void remove(String word) {
    if (this.words.containsKey(word)) {
        this.words.remove(word);
    } else if (this.words.containsValue(word)) {
        String remove = "";
        for (String key : this.words.keySet()) {
            if (this.words.get(key).equals(word)) {
                remove += key;
            }
        }
        this.words.remove(remove);
    }
}

}

1 个答案:

答案 0 :(得分:3)

请注意代码的这一部分,

try {
    writer = new BufferedWriter(new FileWriter(this.file.getName(), true));
    for (String key : this.words.keySet()) {
        writer.write(key + ":" + this.words.get(key) + "\n");
        writer.newLine();
        writer.flush();
        writer.close(); // !!
    }
} catch (Exception e) {

}

在这里,您在close()对象上调用BufferedWriter。在您调用close()后,您无法使用该对象。

  

关闭流后,进一步的write()或flush()调用将导致抛出IOException。

详细了解close() here

此外,由于您正在捕获所有异常并且没有对它们执行任何操作,因此您没有注意到IOException。在未来永远不要这样做。至少记录发生的任何异常。这将帮助您进行调试。