forked from nrkapri/JavaAdvancedTopics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadFileExample.java
More file actions
54 lines (40 loc) · 1.28 KB
/
Copy pathReadFileExample.java
File metadata and controls
54 lines (40 loc) · 1.28 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package java8;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ReadFileExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName = "C:\\Users\\nayank\\Desktop\\README.txt";
//read file into stream, try-with-resources
try (Stream<String> stream = Files.lines(Paths.get(fileName)))
{
//stream.forEach(System.out::println);
//1. filter line 3
//2. convert all content to upper case
//3. convert it into a List
stream
.filter(line -> !line.contains("Oracle"))
.map(String::toUpperCase)
.forEach(x -> System.out.println(x+"\nLINE ENDED====="));
}
catch (Exception e)
{
e.printStackTrace();
}
// 3. BufferedReader + Stream
List<String> list = new ArrayList<>();
try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))) {
//br returns as stream and convert it into a List
list = br.lines().collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
list.forEach(System.out::println);
}
}