|
@@ -2,15 +2,17 @@ package rocks.zipcode;
|
2
|
2
|
|
3
|
3
|
import java.io.*;
|
4
|
4
|
import java.nio.file.Files;
|
|
5
|
+import java.util.ArrayList;
|
5
|
6
|
import java.util.Arrays;
|
6
|
7
|
import java.util.List;
|
|
8
|
+import java.util.regex.Matcher;
|
|
9
|
+import java.util.regex.Pattern;
|
7
|
10
|
|
8
|
11
|
/**
|
9
|
12
|
* @author leon on 16/11/2018.
|
10
|
13
|
*/
|
11
|
14
|
public class Document implements DocumentInterface {
|
12
|
15
|
|
13
|
|
- private final FileWriter fileWriter;
|
14
|
16
|
private final File file;
|
15
|
17
|
|
16
|
18
|
public Document(String fileName) throws IOException {
|
|
@@ -19,46 +21,82 @@ public class Document implements DocumentInterface {
|
19
|
21
|
|
20
|
22
|
public Document(File file) throws IOException {
|
21
|
23
|
this.file = file;
|
22
|
|
- this.fileWriter = new FileWriter(file);
|
23
|
24
|
}
|
24
|
25
|
|
25
|
26
|
@Override
|
26
|
27
|
public void write(String contentToBeWritten) {
|
|
28
|
+ BufferedWriter bw;
|
|
29
|
+ try {
|
|
30
|
+ bw = new BufferedWriter(new FileWriter(file));
|
|
31
|
+ bw.write(contentToBeWritten);
|
|
32
|
+ bw.close();
|
|
33
|
+ } catch (Exception e) {
|
|
34
|
+ e.printStackTrace();
|
|
35
|
+ }
|
|
36
|
+
|
27
|
37
|
}
|
28
|
38
|
|
29
|
39
|
@Override
|
30
|
40
|
public void write(Integer lineNumber, String valueToBeWritten) {
|
|
41
|
+ String fileLine = this.toList().get(lineNumber);
|
|
42
|
+ BufferedWriter bw;
|
|
43
|
+ try {
|
|
44
|
+ bw = new BufferedWriter(new FileWriter(fileLine));
|
|
45
|
+ bw.write(valueToBeWritten);
|
|
46
|
+ bw.close();
|
|
47
|
+ } catch (IOException e) {
|
|
48
|
+ e.printStackTrace();
|
|
49
|
+ }
|
|
50
|
+
|
31
|
51
|
}
|
32
|
52
|
|
33
|
53
|
@Override
|
34
|
54
|
public String read(Integer lineNumber) {
|
35
|
|
- return null;
|
|
55
|
+ return this.toList().get(lineNumber);
|
36
|
56
|
}
|
37
|
57
|
|
38
|
58
|
@Override
|
39
|
59
|
public String read() {
|
40
|
|
- return null;
|
|
60
|
+ String result = "";
|
|
61
|
+ try {
|
|
62
|
+ BufferedReader br = new BufferedReader(new FileReader(file));
|
|
63
|
+ String str = br.readLine();
|
|
64
|
+ while(str != null) {
|
|
65
|
+ result += str;
|
|
66
|
+ result += "\n";
|
|
67
|
+ str = br.readLine();
|
|
68
|
+ }
|
|
69
|
+ } catch (IOException e) {
|
|
70
|
+ e.printStackTrace();
|
|
71
|
+ }
|
|
72
|
+ return result.trim();
|
41
|
73
|
}
|
42
|
74
|
|
43
|
75
|
@Override
|
44
|
76
|
public void replaceAll(String stringToReplace, String replacementString) {
|
|
77
|
+
|
45
|
78
|
}
|
46
|
79
|
|
47
|
80
|
@Override
|
48
|
81
|
public void overWrite(String content) {
|
|
82
|
+ this.write(content);
|
|
83
|
+
|
49
|
84
|
}
|
50
|
85
|
|
51
|
86
|
public List<String> toList() {
|
52
|
|
- return null;
|
|
87
|
+ List<String> list = new ArrayList<>(Arrays.asList(this.read().split("\n")));
|
|
88
|
+ return list;
|
53
|
89
|
}
|
54
|
90
|
|
55
|
91
|
@Override
|
56
|
92
|
public File getFile() {
|
57
|
|
- return null;
|
|
93
|
+ return file;
|
58
|
94
|
}
|
59
|
95
|
|
60
|
96
|
@Override
|
61
|
97
|
public String toString() {
|
62
|
|
- return null;
|
|
98
|
+ return String.valueOf(file) + "{" + this.read() + "}";
|
|
99
|
+// StringBuilder result = new StringBuilder(String.valueOf(file)).append("{").append(this.read()).append("}");
|
|
100
|
+// return result.toString();
|
63
|
101
|
}
|
64
|
102
|
}
|