-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOExample.java
More file actions
113 lines (82 loc) · 3.02 KB
/
IOExample.java
File metadata and controls
113 lines (82 loc) · 3.02 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class IOExample {
public static void main(String[] args) throws IOException {
////////////////////////////////////////////////////////////////////
try {
// read all bytes
byte[] bytes = Files.readAllBytes(Paths.get("C:\\Temp\\input.txt"));
// convert bytes to string
String content = new String(bytes);
// print contents
System.out.println(content);
} catch (IOException ex) {
ex.printStackTrace();
}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
try {
// read all lines
List<String> lines = Files.readAllLines(Paths.get("C:\\Temp\\input.txt"), StandardCharsets.UTF_8);
// print all lines
for (String line : lines) {
System.out.println(line);
}
} catch (IOException ex) {
ex.printStackTrace();
}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
try {
// initialize lines stream
Stream<String> stream = Files.lines(Paths.get("C:\\Temp\\input.txt"), StandardCharsets.UTF_8);
// apply filters and print all lines
stream.map(String::trim).filter(l -> !l.isEmpty()).forEach(System.out::println);
// close the stream
stream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
try {
// data to write
String contents = "Hey, there!\nWhat's up?";
// write to file
Files.write(Paths.get("C:\\Temp\\output.txt"), contents.getBytes());
} catch (IOException ex) {
ex.printStackTrace();
}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
try {
// data to write
String contents = "Weldone!";
// append to file
Files.write(Paths.get("C:\\Temp\\output.txt"), contents.getBytes(), StandardOpenOption.APPEND);
} catch (IOException ex) {
ex.printStackTrace();
}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
try {
// data to write
List<String> contents = Arrays.asList("Hey, there", "How are you doing?");
// write to file
Files.write(Paths.get("C:\\Temp\\output.txt"), contents, StandardCharsets.UTF_8, StandardOpenOption.CREATE);
} catch (IOException ex) {
ex.printStackTrace();
}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// writes all files of the current directory
Files.list(Paths.get(".")).forEach(System.out::println);
}
}