|
@@ -2,6 +2,7 @@ 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;
|
7
|
8
|
|
|
@@ -24,41 +25,81 @@ public class Document implements DocumentInterface {
|
24
|
25
|
|
25
|
26
|
@Override
|
26
|
27
|
public void write(String contentToBeWritten) {
|
|
28
|
+ try {
|
|
29
|
+ BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
|
|
30
|
+ bufferedWriter.write(contentToBeWritten);
|
|
31
|
+ bufferedWriter.close();
|
|
32
|
+ } catch (IOException e) {
|
|
33
|
+ e.printStackTrace();
|
|
34
|
+ }
|
27
|
35
|
}
|
28
|
36
|
|
29
|
37
|
@Override
|
30
|
38
|
public void write(Integer lineNumber, String valueToBeWritten) {
|
|
39
|
+ String[] array = read().split("\n");
|
|
40
|
+ array[lineNumber] = valueToBeWritten;
|
|
41
|
+ String newFile = "";
|
|
42
|
+ for (String element : array) {
|
|
43
|
+ newFile += element + "\n";
|
|
44
|
+ }
|
|
45
|
+ write(newFile.trim());
|
31
|
46
|
}
|
32
|
47
|
|
33
|
48
|
@Override
|
34
|
49
|
public String read(Integer lineNumber) {
|
35
|
|
- return null;
|
|
50
|
+ try {
|
|
51
|
+ BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
|
|
52
|
+ String line = "";
|
|
53
|
+ for (int i = 0; i <= lineNumber; i++) {
|
|
54
|
+ line = bufferedReader.readLine();
|
|
55
|
+ }
|
|
56
|
+ bufferedReader.close();
|
|
57
|
+ return line;
|
|
58
|
+ } catch (IOException e) {
|
|
59
|
+ e.printStackTrace();
|
|
60
|
+ return null;
|
|
61
|
+ }
|
36
|
62
|
}
|
37
|
63
|
|
38
|
64
|
@Override
|
39
|
65
|
public String read() {
|
40
|
|
- return null;
|
|
66
|
+ try {
|
|
67
|
+ BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
|
|
68
|
+ String stringFile = "";
|
|
69
|
+ String line;
|
|
70
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
71
|
+ stringFile += line + "\n";
|
|
72
|
+ }
|
|
73
|
+ bufferedReader.close();
|
|
74
|
+ return stringFile.trim();
|
|
75
|
+ } catch (IOException e) {
|
|
76
|
+ e.printStackTrace();
|
|
77
|
+ return null;
|
|
78
|
+ }
|
41
|
79
|
}
|
42
|
80
|
|
43
|
81
|
@Override
|
44
|
82
|
public void replaceAll(String stringToReplace, String replacementString) {
|
|
83
|
+ String newFile = read().replaceAll(stringToReplace, replacementString);
|
|
84
|
+ write(newFile);
|
45
|
85
|
}
|
46
|
86
|
|
47
|
87
|
@Override
|
48
|
88
|
public void overWrite(String content) {
|
|
89
|
+ write(content);
|
49
|
90
|
}
|
50
|
91
|
|
51
|
92
|
public List<String> toList() {
|
52
|
|
- return null;
|
|
93
|
+ return Arrays.asList(read().split("\n"));
|
53
|
94
|
}
|
54
|
95
|
|
55
|
96
|
@Override
|
56
|
97
|
public File getFile() {
|
57
|
|
- return null;
|
|
98
|
+ return file;
|
58
|
99
|
}
|
59
|
100
|
|
60
|
101
|
@Override
|
61
|
102
|
public String toString() {
|
62
|
|
- return null;
|
|
103
|
+ return file + "{" + read() + "}";
|
63
|
104
|
}
|
64
|
105
|
}
|