|
@@ -2,6 +2,10 @@ package rocks.zipcode;
|
2
|
2
|
|
3
|
3
|
import java.io.*;
|
4
|
4
|
import java.nio.file.Files;
|
|
5
|
+import java.nio.file.Path;
|
|
6
|
+import java.nio.file.Paths;
|
|
7
|
+import java.nio.file.StandardOpenOption;
|
|
8
|
+import java.util.ArrayList;
|
5
|
9
|
import java.util.Arrays;
|
6
|
10
|
import java.util.List;
|
7
|
11
|
|
|
@@ -24,41 +28,102 @@ public class Document implements DocumentInterface {
|
24
|
28
|
|
25
|
29
|
@Override
|
26
|
30
|
public void write(String contentToBeWritten) {
|
|
31
|
+
|
|
32
|
+ try(FileWriter fileWriter = new FileWriter("target/file.txt")) {
|
|
33
|
+ //inherited method from java.io.Writer
|
|
34
|
+ fileWriter.write(contentToBeWritten);
|
|
35
|
+ fileWriter.flush();
|
|
36
|
+ } catch (IOException e) {
|
|
37
|
+ e.printStackTrace();
|
|
38
|
+ }
|
27
|
39
|
}
|
28
|
40
|
|
29
|
41
|
@Override
|
30
|
42
|
public void write(Integer lineNumber, String valueToBeWritten) {
|
|
43
|
+
|
|
44
|
+ try {
|
|
45
|
+ Path path = Paths.get(file.getPath());
|
|
46
|
+ List<String> list = Files.readAllLines(path);
|
|
47
|
+
|
|
48
|
+ String lastLine = list.get(list.size()-1);
|
|
49
|
+ List<String> allButLast = list.subList(0,list.size()-1);
|
|
50
|
+ allButLast.set(lineNumber, valueToBeWritten);
|
|
51
|
+ Files.write(path, allButLast);
|
|
52
|
+ Files.write(path, lastLine.getBytes(), StandardOpenOption.APPEND);
|
|
53
|
+
|
|
54
|
+ } catch (IOException e) {
|
|
55
|
+ e.printStackTrace();
|
|
56
|
+ }
|
31
|
57
|
}
|
32
|
58
|
|
33
|
59
|
@Override
|
34
|
60
|
public String read(Integer lineNumber) {
|
35
|
|
- return null;
|
|
61
|
+
|
|
62
|
+ try {
|
|
63
|
+ return new String(String.valueOf(Files.readAllLines(Paths.get(file.getPath())).get(lineNumber)));
|
|
64
|
+
|
|
65
|
+ } catch (IOException e) {
|
|
66
|
+ e.printStackTrace();
|
|
67
|
+ }
|
|
68
|
+ return null ;
|
36
|
69
|
}
|
37
|
70
|
|
38
|
71
|
@Override
|
39
|
72
|
public String read() {
|
40
|
|
- return null;
|
|
73
|
+
|
|
74
|
+ try {
|
|
75
|
+ return new String(Files.readAllBytes(Paths.get(file.getPath())));
|
|
76
|
+ }
|
|
77
|
+ catch(IOException e) {
|
|
78
|
+ e.printStackTrace();
|
|
79
|
+ }
|
|
80
|
+ return file.getPath();
|
41
|
81
|
}
|
42
|
82
|
|
43
|
83
|
@Override
|
44
|
84
|
public void replaceAll(String stringToReplace, String replacementString) {
|
|
85
|
+
|
|
86
|
+ String doc = read();
|
|
87
|
+ doc = doc.replaceAll(stringToReplace, replacementString);
|
|
88
|
+ write(doc);
|
|
89
|
+
|
|
90
|
+
|
45
|
91
|
}
|
46
|
92
|
|
|
93
|
+
|
47
|
94
|
@Override
|
48
|
95
|
public void overWrite(String content) {
|
|
96
|
+ write(content);
|
49
|
97
|
}
|
50
|
98
|
|
51
|
99
|
public List<String> toList() {
|
52
|
|
- return null;
|
|
100
|
+ String[] arr = read().split("\n");
|
|
101
|
+ List<String> result = new ArrayList<>();
|
|
102
|
+ for(String s: arr) {
|
|
103
|
+ result.add(s);
|
|
104
|
+ }
|
|
105
|
+ return result;
|
53
|
106
|
}
|
54
|
107
|
|
55
|
108
|
@Override
|
56
|
109
|
public File getFile() {
|
57
|
|
- return null;
|
|
110
|
+
|
|
111
|
+ return this.file;
|
58
|
112
|
}
|
59
|
113
|
|
60
|
114
|
@Override
|
61
|
115
|
public String toString() {
|
|
116
|
+ try {
|
|
117
|
+ Path path = Paths.get(file.getPath());
|
|
118
|
+ List<String> result = Files.readAllLines(path);
|
|
119
|
+ String list = result.toString().replace("[", "").replace("]", "");
|
|
120
|
+
|
|
121
|
+ return path + "{" +list + "}";
|
|
122
|
+ }
|
|
123
|
+ catch(IOException e) {
|
|
124
|
+ e.printStackTrace();
|
|
125
|
+ }
|
62
|
126
|
return null;
|
|
127
|
+
|
63
|
128
|
}
|
64
|
129
|
}
|