Languages
[Edit]
EN

Java read all text from file in single line of code

5 points
Created by:
illona
316

Quick solution:

String text = new String(Files.readAllBytes(Paths.get("C:\\projects\\text_file.txt")));

Practical example:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ReadTextExample {

    public static void main(String[] args) throws IOException {

        Path path = Paths.get("C:\\projects\\text_file.txt");

        String entireTextFile = new String(Files.readAllBytes(path));

        System.out.println(entireTextFile);
    }
}

Output:

line 1
line 2
line 3

See also:

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join