Java IO & NIO Java
This tutorial shows how to write/read lines to a file.
Writing lines
1. Using Files class method
There are two methods that can help writing
public static Path write(Path path, byte[] bytes, OpenOption... options)
public static Path write(Path path, Iterable extends CharSequence> lines, OpenOption... options)
2. Using BufferedWriter
For large files it's recommended to use this method for performance.
public void write(String str)
Reading lines
1. Using Files class method
public static List<String> readAllLines(Path path)
public static List<String> readAllLines(Path path, Charset cs)
public static Stream<String> lines(Path path)
public static Stream<String> lines(Path path, Charset cs)
2. Using BufferedReader
public String readLine()
public List<String> readAllLines()
public Stream<String> lines()
Examples
Using Files methods
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);
}
}Outputchokecherry dewberry boysenberry
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;
public class ReadWriteLinesUtil2 {
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
Files.write(path, lines);
//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);
}
}
Outputchokecherry dewberry boysenberry
Using BufferedWriter
package com.logicbig.example;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
public class BufferedWriterExample {
public static void main(String[] args) throws IOException {
List<String> lines = Arrays.asList("chokecherry", "dewberry", "boysenberry");
Path path = Files.createTempFile("my-file", ".txt");
//writing lines
try (BufferedWriter writer = new BufferedWriter(new FileWriter(path.toFile()))) {
lines.forEach(line -> {
try {
writer.write(line);
writer.newLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
} catch (IOException e) {
e.printStackTrace();
}
//reading lines
try (BufferedReader reader = Files.newBufferedReader(path)) {
reader.lines().forEach(System.out::println);
}
}
}
Outputchokecherry dewberry boysenberry
|