How to read a text file in Java

[CG]BOUGA
1,807 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
1
20
21
22
23
24
25
26
27
28
29
34
// {...}
// Method 1: for small files (load the entire file in memory)
List<String> lines = Files.readAllLines(Paths.get("file.txt"));
// Method 2: for big files (read line by line)
try (BufferedReader reader = Files.newBufferedReader(Paths.get("file.txt"), StandardCharsets.UTF_8)) {
reader.lines().forEach(System.out::println);
}
//{...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content