forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
38 lines (26 loc) · 1.08 KB
/
Main.java
File metadata and controls
38 lines (26 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package modern.challenge;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class Main {
private static final String FILE_PATH = "D:/learning/packt/resources.txt";
public static void main(String[] args) {
System.out.println("Using Files.lines(): ");
try (Stream<String> filesStream = Files.lines(
Paths.get(FILE_PATH), StandardCharsets.UTF_8)) {
filesStream.forEach(System.out::println);
} catch (IOException e) {
// handle IOException if needed, otherwise remove the catch block
}
System.out.println("\nUsing BufferedReader.lines(): ");
try (BufferedReader brStream = Files.newBufferedReader(
Paths.get(FILE_PATH), StandardCharsets.UTF_8)) {
brStream.lines().forEach(System.out::println);
} catch (IOException e) {
// handle IOException if needed, otherwise remove the catch block
}
}
}