Java - How Can I Write My ArrayList to a file, and Read (load) that file to the original ArrayList?

后端 未结 6 1490
时光取名叫无心
时光取名叫无心 2020-11-27 15:05

I am writing a program in Java which displays a range of afterschool clubs (E.G. Football, Hockey - entered by user). The clubs are added into the following ArrayList<

6条回答
  •  Happy的楠姐
    2020-11-27 15:32

    In Java 8 you can use Files.write() method with two arguments: Path and List, something like this:

    List clubNames = clubs.stream()
        .map(Club::getName)
        .collect(Collectors.toList())
    
    try {
        Files.write(Paths.get(fileName), clubNames);
    } catch (IOException e) {
        log.error("Unable to write out names", e);
    }
    

提交回复
热议问题