我想将 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/