有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从中获取地图。txt文件使用属性?

这是写哈希表的代码。txt文件

public static void save(String filename, Map<String, String> hashtable) throws IOException {
    Properties prop = new Properties();
    prop.putAll(hashtable);
    FileOutputStream fos = new FileOutputStream(filename);
    try {
       prop.store(fos, prop);
    } finally {
       fos.close();
    }
}

我们如何从该文件中取回哈希表? 谢谢


共 (1) 个答案

  1. # 1 楼答案

    以同样丑陋的方式:

    @SuppressWarnings("unchecked")
    public static Map<String, String> load(String filename) throws IOException {
        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream(filename);
        try {
            prop.load(fis);
        } finally {
            fis.close();
        }
        return (Map) prop;
    }