|
|
@@ -1,5 +1,7 @@
|
|
1
|
1
|
package rocks.zipcode;
|
|
2
|
2
|
|
|
|
3
|
+import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
|
|
|
4
|
+
|
|
3
|
5
|
import java.io.*;
|
|
4
|
6
|
import java.nio.charset.StandardCharsets;
|
|
5
|
7
|
import java.nio.file.Files;
|
|
|
@@ -9,6 +11,7 @@ import java.util.ArrayList;
|
|
9
|
11
|
import java.util.Arrays;
|
|
10
|
12
|
import java.util.LinkedList;
|
|
11
|
13
|
import java.util.List;
|
|
|
14
|
+import java.util.regex.Matcher;
|
|
12
|
15
|
import java.util.regex.Pattern;
|
|
13
|
16
|
import java.util.stream.Stream;
|
|
14
|
17
|
|
|
|
@@ -17,8 +20,8 @@ import java.util.stream.Stream;
|
|
17
|
20
|
*/
|
|
18
|
21
|
public class Document implements DocumentInterface {
|
|
19
|
22
|
|
|
20
|
|
- private final FileWriter fileWriter;
|
|
21
|
23
|
private final File file;
|
|
|
24
|
+ private List<String> lines;
|
|
22
|
25
|
|
|
23
|
26
|
public Document(String fileName) throws IOException {
|
|
24
|
27
|
this(new File(fileName));
|
|
|
@@ -26,12 +29,13 @@ public class Document implements DocumentInterface {
|
|
26
|
29
|
|
|
27
|
30
|
public Document(File file) throws IOException {
|
|
28
|
31
|
this.file = file;
|
|
29
|
|
- this.fileWriter = new FileWriter(file);
|
|
30
|
32
|
}
|
|
31
|
33
|
|
|
32
|
34
|
@Override
|
|
33
|
35
|
public void write(String contentToBeWritten) {
|
|
|
36
|
+
|
|
34
|
37
|
try {
|
|
|
38
|
+ FileWriter fileWriter = new FileWriter(file);
|
|
35
|
39
|
fileWriter.write(contentToBeWritten);
|
|
36
|
40
|
fileWriter.close();
|
|
37
|
41
|
} catch (IOException e) {
|
|
|
@@ -41,23 +45,16 @@ public class Document implements DocumentInterface {
|
|
41
|
45
|
|
|
42
|
46
|
@Override
|
|
43
|
47
|
public void write(Integer lineNumber, String valueToBeWritten){
|
|
44
|
|
- List<String> lines = toList();
|
|
|
48
|
+ String result = "";
|
|
|
49
|
+ lines = toList();
|
|
45
|
50
|
lines.set(lineNumber, valueToBeWritten);
|
|
46
|
|
- try {
|
|
47
|
|
- Files.write(file.toPath(), lines);
|
|
48
|
|
- } catch (IOException e){
|
|
49
|
|
- System.out.println("error");
|
|
50
|
|
- }
|
|
|
51
|
+ write(result.join("\n", lines));
|
|
|
52
|
+
|
|
51
|
53
|
}
|
|
52
|
54
|
|
|
53
|
55
|
@Override
|
|
54
|
56
|
public String read(Integer lineNumber) {
|
|
55
|
|
- String result = "";
|
|
56
|
|
- List<String> lines = toList();
|
|
57
|
|
- for (int i = 0; i < lines.size(); i++) {
|
|
58
|
|
- if (i == lineNumber) {result = lines.get(i); }
|
|
59
|
|
- }
|
|
60
|
|
- return result;
|
|
|
57
|
+ return toList().get(lineNumber);
|
|
61
|
58
|
}
|
|
62
|
59
|
|
|
63
|
60
|
@Override
|
|
|
@@ -72,16 +69,19 @@ public class Document implements DocumentInterface {
|
|
72
|
69
|
}
|
|
73
|
70
|
|
|
74
|
71
|
@Override
|
|
75
|
|
- public void replaceAll(String stringToReplace, String replacementString) {
|
|
76
|
|
-
|
|
|
72
|
+ public void replaceAll(String stringToReplace, String replacementString){
|
|
|
73
|
+ Pattern pattern = Pattern.compile(stringToReplace);
|
|
|
74
|
+ Matcher matcher = pattern.matcher(read());
|
|
|
75
|
+ write(matcher.replaceAll(replacementString));
|
|
77
|
76
|
}
|
|
78
|
77
|
|
|
79
|
78
|
@Override
|
|
80
|
79
|
public void overWrite(String content) {
|
|
|
80
|
+ write(content);
|
|
81
|
81
|
}
|
|
82
|
82
|
|
|
83
|
83
|
public List<String> toList() {
|
|
84
|
|
- List<String> lines = new ArrayList(){};
|
|
|
84
|
+ lines = new ArrayList(){};
|
|
85
|
85
|
try {
|
|
86
|
86
|
lines = Files.readAllLines(file.toPath());
|
|
87
|
87
|
} catch (IOException e) {
|