|
@@ -1,20 +1,29 @@
|
1
|
1
|
package rocks.zipcode;
|
2
|
2
|
|
3
|
3
|
import java.io.*;
|
|
4
|
+import java.net.URI;
|
|
5
|
+import java.nio.charset.Charset;
|
|
6
|
+import java.nio.charset.StandardCharsets;
|
4
|
7
|
import java.nio.file.Files;
|
5
|
|
-import java.util.Arrays;
|
|
8
|
+import java.nio.file.Path;
|
|
9
|
+import java.nio.file.Paths;
|
|
10
|
+import java.util.ArrayList;
|
6
|
11
|
import java.util.List;
|
|
12
|
+import java.util.stream.Collectors;
|
|
13
|
+import java.util.stream.Stream;
|
7
|
14
|
|
8
|
15
|
/**
|
9
|
16
|
* @author leon on 16/11/2018.
|
10
|
17
|
*/
|
11
|
18
|
public class Document implements DocumentInterface {
|
12
|
19
|
|
|
20
|
+ //have to implement all abstract methods
|
|
21
|
+
|
13
|
22
|
private final FileWriter fileWriter;
|
14
|
23
|
private final File file;
|
15
|
24
|
|
16
|
25
|
public Document(String fileName) throws IOException {
|
17
|
|
- this(new File(fileName));
|
|
26
|
+ this(new File(fileName)); // have to open all the file name to read and write
|
18
|
27
|
}
|
19
|
28
|
|
20
|
29
|
public Document(File file) throws IOException {
|
|
@@ -23,42 +32,113 @@ public class Document implements DocumentInterface {
|
23
|
32
|
}
|
24
|
33
|
|
25
|
34
|
@Override
|
26
|
|
- public void write(String contentToBeWritten) {
|
|
35
|
+ public void write(String contentToBeWritten) throws IOException {
|
|
36
|
+ try {
|
|
37
|
+ fileWriter.write(contentToBeWritten);
|
|
38
|
+ fileWriter.flush();
|
|
39
|
+ } catch (IOException e) {
|
|
40
|
+ e.printStackTrace();
|
|
41
|
+ }
|
27
|
42
|
}
|
28
|
43
|
|
29
|
44
|
@Override
|
30
|
45
|
public void write(Integer lineNumber, String valueToBeWritten) {
|
31
|
|
- }
|
|
46
|
+ List <String> lines = null;
|
|
47
|
+ try {
|
|
48
|
+ lines = Files.readAllLines(file.toPath());
|
|
49
|
+ lines.set(lineNumber,valueToBeWritten);
|
|
50
|
+ Files.write(file.toPath(), lines);
|
|
51
|
+ } catch (IOException e) {
|
|
52
|
+ e.printStackTrace();
|
|
53
|
+ }
|
32
|
54
|
|
|
55
|
+ }
|
33
|
56
|
@Override
|
34
|
|
- public String read(Integer lineNumber) {
|
35
|
|
- return null;
|
|
57
|
+ public String read(Integer lineNumber) throws IOException {
|
|
58
|
+
|
|
59
|
+ try(Stream<String> lines = Files.lines(Paths.get("target/file.txt"))){
|
|
60
|
+ String line = lines.skip(lineNumber).findFirst().get();
|
|
61
|
+ return line;
|
|
62
|
+ }
|
|
63
|
+
|
36
|
64
|
}
|
37
|
65
|
|
38
|
66
|
@Override
|
39
|
67
|
public String read() {
|
40
|
|
- return null;
|
|
68
|
+
|
|
69
|
+ StringBuilder builder = new StringBuilder();
|
|
70
|
+ try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
|
71
|
+ String sCurrentLine;
|
|
72
|
+ while ((sCurrentLine = br.readLine()) != null) {
|
|
73
|
+ builder.append(sCurrentLine).append("\n");
|
|
74
|
+ }
|
|
75
|
+ } catch (IOException e) {
|
|
76
|
+ e.printStackTrace();
|
|
77
|
+ }
|
|
78
|
+ return builder.toString().trim();
|
41
|
79
|
}
|
42
|
80
|
|
43
|
81
|
@Override
|
44
|
82
|
public void replaceAll(String stringToReplace, String replacementString) {
|
|
83
|
+ Path path = Paths.get("target/file.txt");
|
|
84
|
+ Charset charset = StandardCharsets.UTF_8;
|
|
85
|
+
|
|
86
|
+ String content = null;
|
|
87
|
+ try {
|
|
88
|
+ content = new String(Files.readAllBytes(path), charset);
|
|
89
|
+ } catch (IOException e) {
|
|
90
|
+ e.printStackTrace();
|
|
91
|
+ }
|
|
92
|
+ content = content.replaceAll(stringToReplace, replacementString);
|
|
93
|
+ try {
|
|
94
|
+ Files.write(path, content.getBytes(charset));
|
|
95
|
+ } catch (IOException e) {
|
|
96
|
+ e.printStackTrace();
|
|
97
|
+ }
|
45
|
98
|
}
|
46
|
99
|
|
47
|
100
|
@Override
|
48
|
101
|
public void overWrite(String content) {
|
|
102
|
+
|
|
103
|
+ try {
|
|
104
|
+ Writer fileWriter = new FileWriter("target/file.txt", false);
|
|
105
|
+ write(content);
|
|
106
|
+ } catch (IOException e) {
|
|
107
|
+ e.printStackTrace();
|
|
108
|
+ }
|
49
|
109
|
}
|
50
|
110
|
|
51
|
|
- public List<String> toList() {
|
|
111
|
+ public List<String> toList() throws IOException {
|
|
112
|
+
|
52
|
113
|
return null;
|
53
|
114
|
}
|
54
|
115
|
|
|
116
|
+
|
55
|
117
|
@Override
|
56
|
118
|
public File getFile() {
|
57
|
|
- return null;
|
|
119
|
+
|
|
120
|
+ return file;
|
58
|
121
|
}
|
59
|
122
|
|
60
|
123
|
@Override
|
61
|
124
|
public String toString() {
|
62
|
|
- return null;
|
|
125
|
+
|
|
126
|
+ String fileName = "target/file.txt";
|
|
127
|
+ String content = "";
|
|
128
|
+
|
|
129
|
+ try {
|
|
130
|
+ content = new String(Files.readAllBytes(Paths.get("target/file.txt")));
|
|
131
|
+
|
|
132
|
+ } catch (IOException e) {
|
|
133
|
+ e.printStackTrace();
|
|
134
|
+ }
|
|
135
|
+ StringBuilder builder = new StringBuilder(fileName)
|
|
136
|
+ .append('{')
|
|
137
|
+ .append(content)
|
|
138
|
+ .append('}');
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+ return builder.toString();
|
63
|
142
|
}
|
|
143
|
+
|
64
|
144
|
}
|