0

I have 5 records in pList in the ArrayList below but why does it only print the last record to file ?

I put the try first and the for loop inside it and that worked but trying to understand the logic why doesn't the code below work ?

for(Person p: pList){
    try(PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("persons.txt")))){
        pw.println(p.toString());
    }
    catch(IOException ex){
        System.out.println(ex.getMessage());
    }
}
neilnm
  • 183
  • 1
  • 3
  • 8

0 Answers0