Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto save HashSet<String> to .txt?

I want to store the HashSet to the server directory. But i'm now only been able to store it in .bin files. But how do I print all the Key's in the HashSet to a .txt file?

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();
}
like image 269
user1621988 Avatar asked Dec 03 '22 01:12

user1621988


1 Answers

// 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();
like image 106
aviad Avatar answered Dec 05 '22 13:12

aviad