Java IO & NIO Java
This tutorial shows how to write/read lines to a file.
package com.logicbig.example; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class ReadWriteLinesUtil { public static void main(String[] args) throws IOException { List<String> lines = Arrays.asList("chokecherry", "dewberry", "boysenberry"); Path path = Files.createTempFile("my-file", ".txt"); //saving lines writeLines(lines, path); //reading lines readLines(path); path.toFile().deleteOnExit(); } private static void readLines(Path path) throws IOException { List<String> lines = Files.readAllLines(path); lines.forEach(System.out::println); } private static void writeLines(List<String> list, Path path) throws IOException { byte[] bytes = list.stream().collect(Collectors.joining("\n")).getBytes(); Files.write(path, bytes); } }
chokecherrydewberryboysenberry