java - 如何将 HashSet<String> 保存到 .txt?

标签 java string file-io set hashset

我想将 HashSet 存储到服务器目录。 但我现在只能将其存储在 .bin 文件中。 但是如何将 HashSet 中的所有键打印到一个 .txt 文件中呢?

static Set<String> MapLocation = new HashSet<String>();

    try {
        SLAPI.save(MapLocation, "MapLocation.bin");
    } catch (Exception ex) {

    }

public static void save(Object obj, String path) throws Exception {
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
            path));
    oos.writeObject(obj);
    oos.flush();
    oos.close();
}

最佳答案

// check IOException in method signature
BufferedWriter out = new BufferedWriter(new FileWriter(path));
Iterator it = MapLocation.iterator(); // why capital "M"?
while(it.hasNext()) {
    out.write(it.next());
    out.newLine();
}
out.close();

关于java - 如何将 HashSet<String> 保存到 .txt?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12996199/

相关文章:

java - 随机访问文件上的输入/输出流,关闭是否传播?

java - 如何找到图像的中心?

java - 如何删除目录中上传的相同文件?

java - Netbeans dist jar 文件给出 NoClassDefFoundError

python - 在 python 中将字符串转换为数组的最快方法是什么?

c++ - 我如何使用 ' string::find ' 使用 C++ 在文件中查找单词

c# - 用空格和#字符替换#字符

c - 运行具有重定向输入的 C 程序时如何知道是否 EOF?

java - 使用 Jackson 序列化和反序列化 Java 泛型 : StackOverflowError

java - 我可以将 php 和 java 用于同一个 Web 应用程序吗