Close

Java IO - How to write lines To a file and read lines from a files?

[Last Updated: Jan 19, 2026]

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 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);
    }
}

Output

chokecherry
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);
    }
}

Output

chokecherry
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);
        }
    }
}

Output

chokecherry
dewberry
boysenberry

See Also

Join