1

I have a HashMap that I would like to write as separate lines to a text file. How do you go about doing this? In the SysOut above the code for writing to a text file I am printing out the values in the way that I would like to print them.

    map.forEach((k,v)-> System.out.println(k+", "+v));

    File file = new File(Constants.FILEPATH);

    FileOutputStream f = new FileOutputStream(file);  
    ObjectOutputStream s = new ObjectOutputStream(f);          
    s.writeObject(map);
    s.close();
Nazrod12
  • 111
  • 1
  • 2
  • 9

1 Answers1

2

Don't use an ObjectOutputStream, use a PrintWriter:

try (PrintWriter out = new PrintWriter(file)) {
    map.forEach((k,v) -> out.println(k+", "+v));
}
teppic
  • 7,051
  • 1
  • 29
  • 35