|
|
@@ -1,9 +1,17 @@
|
|
1
|
1
|
package rocks.zipcode;
|
|
2
|
2
|
|
|
|
3
|
+import org.apache.commons.io.FileUtils;
|
|
|
4
|
+
|
|
3
|
5
|
import java.io.*;
|
|
|
6
|
+import java.net.URI;
|
|
|
7
|
+import java.nio.charset.StandardCharsets;
|
|
4
|
8
|
import java.nio.file.Files;
|
|
|
9
|
+import java.nio.file.Path;
|
|
|
10
|
+import java.nio.file.Paths;
|
|
|
11
|
+import java.nio.file.StandardOpenOption;
|
|
5
|
12
|
import java.util.Arrays;
|
|
6
|
13
|
import java.util.List;
|
|
|
14
|
+import java.util.stream.Stream;
|
|
7
|
15
|
|
|
8
|
16
|
/**
|
|
9
|
17
|
* @author leon on 16/11/2018.
|
|
|
@@ -24,28 +32,64 @@ public class Document implements DocumentInterface {
|
|
24
|
32
|
|
|
25
|
33
|
@Override
|
|
26
|
34
|
public void write(String contentToBeWritten) {
|
|
|
35
|
+ try {
|
|
|
36
|
+ fileWriter.write(contentToBeWritten);
|
|
|
37
|
+ fileWriter.flush();
|
|
|
38
|
+ } catch (IOException e){
|
|
|
39
|
+ e.printStackTrace();
|
|
|
40
|
+ }
|
|
27
|
41
|
}
|
|
28
|
42
|
|
|
29
|
43
|
@Override
|
|
30
|
44
|
public void write(Integer lineNumber, String valueToBeWritten) {
|
|
|
45
|
+ try {
|
|
|
46
|
+ Path path = Paths.get("target/file.txt");
|
|
|
47
|
+ List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
|
|
|
48
|
+ lines.set(lineNumber , valueToBeWritten);
|
|
|
49
|
+ Files.write(path, lines.subList(0, lines.size()-1), StandardCharsets.UTF_8);
|
|
|
50
|
+ Files.write(path, lines.get(lines.size()-1).getBytes("UTF-8"), StandardOpenOption.APPEND);
|
|
|
51
|
+ } catch (IOException e){
|
|
|
52
|
+ e.printStackTrace();
|
|
|
53
|
+ }
|
|
31
|
54
|
}
|
|
32
|
55
|
|
|
33
|
56
|
@Override
|
|
34
|
|
- public String read(Integer lineNumber) {
|
|
35
|
|
- return null;
|
|
|
57
|
+ public String read(Integer lineNumber) throws IOException {
|
|
|
58
|
+ return Files.readAllLines(file.toPath()).get(lineNumber);
|
|
36
|
59
|
}
|
|
37
|
60
|
|
|
38
|
61
|
@Override
|
|
39
|
|
- public String read() {
|
|
40
|
|
- return null;
|
|
|
62
|
+ public String read() throws IOException{
|
|
|
63
|
+
|
|
|
64
|
+ return new String(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
|
|
|
65
|
+
|
|
41
|
66
|
}
|
|
42
|
67
|
|
|
43
|
68
|
@Override
|
|
44
|
69
|
public void replaceAll(String stringToReplace, String replacementString) {
|
|
|
70
|
+ try {
|
|
|
71
|
+ Path path = Paths.get("/Users/zavonmalone/labs/exceptions-readandwrite/target/file.txt");
|
|
|
72
|
+ String content = new String(Files.readAllBytes(path),StandardCharsets.UTF_8);
|
|
|
73
|
+ content = content.replaceAll(stringToReplace, replacementString);
|
|
|
74
|
+ Files.write(path, content.getBytes(StandardCharsets.UTF_8));
|
|
|
75
|
+
|
|
|
76
|
+ }catch(IOException e){
|
|
|
77
|
+ e.printStackTrace();
|
|
|
78
|
+ }
|
|
|
79
|
+
|
|
45
|
80
|
}
|
|
46
|
81
|
|
|
47
|
82
|
@Override
|
|
48
|
83
|
public void overWrite(String content) {
|
|
|
84
|
+ try {
|
|
|
85
|
+ Path path = Paths.get("target/file.txt");
|
|
|
86
|
+ List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
|
|
|
87
|
+ lines.clear();
|
|
|
88
|
+ lines.add(content);
|
|
|
89
|
+ Files.write(path, lines.subList(0, lines.size()-1), StandardCharsets.UTF_8);
|
|
|
90
|
+ Files.write(path, lines.get(lines.size()-1).getBytes("UTF-8"), StandardOpenOption.APPEND); } catch (IOException e) {
|
|
|
91
|
+ e.printStackTrace();
|
|
|
92
|
+ }
|
|
49
|
93
|
}
|
|
50
|
94
|
|
|
51
|
95
|
public List<String> toList() {
|
|
|
@@ -54,11 +98,17 @@ public class Document implements DocumentInterface {
|
|
54
|
98
|
|
|
55
|
99
|
@Override
|
|
56
|
100
|
public File getFile() {
|
|
|
101
|
+ Path path = Paths.get("/Users/zavonmalone/labs/exceptions-readandwrite/target/file.txt");
|
|
57
|
102
|
return null;
|
|
58
|
103
|
}
|
|
59
|
104
|
|
|
60
|
105
|
@Override
|
|
61
|
106
|
public String toString() {
|
|
|
107
|
+ try {
|
|
|
108
|
+ return file.toString() + "{" +read() + "}";
|
|
|
109
|
+ }catch(IOException e){
|
|
|
110
|
+ e.printStackTrace();
|
|
|
111
|
+ }
|
|
62
|
112
|
return null;
|
|
63
|
113
|
}
|
|
64
|
114
|
}
|