|
@@ -1,7 +1,11 @@
|
1
|
1
|
package rocks.zipcode;
|
2
|
2
|
|
3
|
3
|
import java.io.*;
|
|
4
|
+import java.nio.charset.StandardCharsets;
|
4
|
5
|
import java.nio.file.Files;
|
|
6
|
+import java.nio.file.Path;
|
|
7
|
+import java.nio.file.Paths;
|
|
8
|
+import java.nio.file.StandardOpenOption;
|
5
|
9
|
import java.util.Arrays;
|
6
|
10
|
import java.util.List;
|
7
|
11
|
|
|
@@ -14,51 +18,139 @@ public class Document implements DocumentInterface {
|
14
|
18
|
private final File file;
|
15
|
19
|
|
16
|
20
|
public Document(String fileName) throws IOException {
|
|
21
|
+
|
17
|
22
|
this(new File(fileName));
|
18
|
23
|
}
|
19
|
24
|
|
20
|
25
|
public Document(File file) throws IOException {
|
|
26
|
+
|
21
|
27
|
this.file = file;
|
22
|
28
|
this.fileWriter = new FileWriter(file);
|
23
|
29
|
}
|
24
|
30
|
|
25
|
31
|
@Override
|
26
|
32
|
public void write(String contentToBeWritten) {
|
|
33
|
+
|
|
34
|
+ try {
|
|
35
|
+ fileWriter.write(contentToBeWritten);
|
|
36
|
+
|
|
37
|
+ fileWriter.flush();
|
|
38
|
+
|
|
39
|
+ } catch (IOException e) {
|
|
40
|
+
|
|
41
|
+ e.printStackTrace();
|
|
42
|
+
|
|
43
|
+ throw new IllegalArgumentException();
|
|
44
|
+
|
|
45
|
+ }catch (IllegalArgumentException iae) {
|
|
46
|
+
|
|
47
|
+ iae.printStackTrace();
|
|
48
|
+ }
|
27
|
49
|
}
|
28
|
50
|
|
29
|
51
|
@Override
|
30
|
52
|
public void write(Integer lineNumber, String valueToBeWritten) {
|
|
53
|
+
|
|
54
|
+ try {
|
|
55
|
+ Path path = Paths.get("/Users/xzaviacuello/Labs/exceptions-readandwrite/target/file.txt");
|
|
56
|
+
|
|
57
|
+ List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
|
|
58
|
+
|
|
59
|
+ lines.set(lineNumber, valueToBeWritten);
|
|
60
|
+
|
|
61
|
+ Files.write(path, lines.subList(0, lines.size() - 1), StandardCharsets.UTF_8);
|
|
62
|
+
|
|
63
|
+ Files.write(path, lines.get(lines.size() - 1).getBytes("UTF-8"), StandardOpenOption.APPEND);
|
|
64
|
+
|
|
65
|
+ }catch (IllegalArgumentException e) {
|
|
66
|
+
|
|
67
|
+ e.printStackTrace();
|
|
68
|
+
|
|
69
|
+ }catch (IOException io){
|
|
70
|
+
|
|
71
|
+ io.printStackTrace();
|
|
72
|
+ }
|
31
|
73
|
}
|
32
|
74
|
|
33
|
75
|
@Override
|
34
|
|
- public String read(Integer lineNumber) {
|
35
|
|
- return null;
|
|
76
|
+ public String read(Integer lineNumber) throws IOException {
|
|
77
|
+
|
|
78
|
+ return Files.readAllLines(file.toPath()).get(lineNumber);
|
36
|
79
|
}
|
37
|
80
|
|
38
|
81
|
@Override
|
39
|
|
- public String read() {
|
40
|
|
- return null;
|
|
82
|
+ public String read() throws IOException {
|
|
83
|
+
|
|
84
|
+ return new String(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
|
|
85
|
+
|
|
86
|
+
|
41
|
87
|
}
|
42
|
88
|
|
43
|
89
|
@Override
|
44
|
90
|
public void replaceAll(String stringToReplace, String replacementString) {
|
|
91
|
+
|
|
92
|
+ try{
|
|
93
|
+ Path path = Paths.get("/Users/xzaviacuello/Labs/exceptions-readandwrite/target/file.txt");
|
|
94
|
+
|
|
95
|
+ String content = new String(Files.readAllBytes(path),StandardCharsets.UTF_8);
|
|
96
|
+
|
|
97
|
+ content = content.replaceAll(stringToReplace, replacementString);
|
|
98
|
+
|
|
99
|
+ Files.write(path, content.getBytes(StandardCharsets.UTF_8));
|
|
100
|
+
|
|
101
|
+ }catch(IOException e) {
|
|
102
|
+
|
|
103
|
+ e.printStackTrace();
|
|
104
|
+ }
|
45
|
105
|
}
|
46
|
106
|
|
47
|
107
|
@Override
|
48
|
108
|
public void overWrite(String content) {
|
|
109
|
+
|
|
110
|
+ try {
|
|
111
|
+ Path path = Paths.get("/Users/xzaviacuello/Labs/exceptions-readandwrite/target/file.txt");
|
|
112
|
+
|
|
113
|
+ List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
|
|
114
|
+
|
|
115
|
+ lines.clear();
|
|
116
|
+
|
|
117
|
+ lines.add(content);
|
|
118
|
+
|
|
119
|
+ Files.write(path, lines.subList(0, lines.size() - 1), StandardCharsets.UTF_8);
|
|
120
|
+
|
|
121
|
+ Files.write(path, lines.get(lines.size() - 1).getBytes("UTF-8"), StandardOpenOption.APPEND);
|
|
122
|
+
|
|
123
|
+ }catch (IOException io){
|
|
124
|
+
|
|
125
|
+ io.printStackTrace();
|
|
126
|
+ }
|
49
|
127
|
}
|
50
|
128
|
|
51
|
129
|
public List<String> toList() {
|
|
130
|
+
|
52
|
131
|
return null;
|
53
|
132
|
}
|
54
|
133
|
|
55
|
134
|
@Override
|
56
|
135
|
public File getFile() {
|
|
136
|
+
|
|
137
|
+ Path path = Paths.get("/Users/xzaviacuello/Labs/exceptions-readandwrite/target/file.txt");
|
|
138
|
+
|
57
|
139
|
return null;
|
58
|
140
|
}
|
59
|
141
|
|
60
|
142
|
@Override
|
61
|
143
|
public String toString() {
|
|
144
|
+
|
|
145
|
+ try{
|
|
146
|
+
|
|
147
|
+ return file.toString() + "{" + read() + "}";
|
|
148
|
+
|
|
149
|
+ }catch (IOException e) {
|
|
150
|
+
|
|
151
|
+ e.printStackTrace();
|
|
152
|
+ }
|
|
153
|
+
|
62
|
154
|
return null;
|
63
|
155
|
}
|
64
|
156
|
}
|