forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadWrite.java
More file actions
78 lines (55 loc) · 2.01 KB
/
ReadWrite.java
File metadata and controls
78 lines (55 loc) · 2.01 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
package com.zetcode;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
package com.zetcode;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Instant;
public class ReadWrite {
public static void main(String[] args) throws IOException {
var content = Files.readString(Paths.get("src/resources/test.txt"));
System.out.println(content);
System.out.println("***********************************");
var lines = Files.readAllLines(Paths.get("src/resources/test.txt"));
System.out.println(lines);
System.out.println("***********************************");
var path = Paths.get("src/resources/test.txt");
try (var br = Files.newBufferedReader(path)) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
var line1 = "C# is cool";
var line2 = Instant.now().toString();
try (var br = new BufferedWriter(new FileWriter(("src/resources/test2.txt")))) {
br.write(line1);
br.newLine();
br.write(line2);
br.newLine();
}
try (var br = new BufferedReader(new FileReader(("src/resources/test.txt")))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
var text = "old falcon";
var data = text.getBytes();
try (var fos = new FileOutputStream("src/resources/test.txt")) {
fos.write(data);
}
try (var fr = new FileWriter("src/resources/test2.txt")) {
fr.write("old falcon");
}
try (var pw = new PrintWriter("src/resources/test.txt")) {
pw.println("Java is cool");
pw.printf("Today is %s%n", Instant.now());
}
}
}