|
|
@@ -1,9 +1,15 @@
|
|
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.util.ArrayList;
|
|
5
|
9
|
import java.util.Arrays;
|
|
|
10
|
+import java.util.LinkedList;
|
|
6
|
11
|
import java.util.List;
|
|
|
12
|
+import java.util.stream.Stream;
|
|
7
|
13
|
|
|
8
|
14
|
/**
|
|
9
|
15
|
* @author leon on 16/11/2018.
|
|
|
@@ -24,10 +30,23 @@ public class Document implements DocumentInterface {
|
|
24
|
30
|
|
|
25
|
31
|
@Override
|
|
26
|
32
|
public void write(String contentToBeWritten) {
|
|
|
33
|
+ try {
|
|
|
34
|
+ fileWriter.write(contentToBeWritten);
|
|
|
35
|
+ fileWriter.close();
|
|
|
36
|
+ } catch (IOException e) {
|
|
|
37
|
+ e.printStackTrace();
|
|
|
38
|
+ }
|
|
27
|
39
|
}
|
|
28
|
40
|
|
|
29
|
41
|
@Override
|
|
30
|
|
- public void write(Integer lineNumber, String valueToBeWritten) {
|
|
|
42
|
+ public void write(Integer lineNumber, String valueToBeWritten){
|
|
|
43
|
+ List<String> lines = toList();
|
|
|
44
|
+ lines.set(lineNumber, valueToBeWritten);
|
|
|
45
|
+ try {
|
|
|
46
|
+ Files.write(file.toPath(), lines);
|
|
|
47
|
+ } catch (IOException e){
|
|
|
48
|
+ System.out.println("error");
|
|
|
49
|
+ }
|
|
31
|
50
|
}
|
|
32
|
51
|
|
|
33
|
52
|
@Override
|
|
|
@@ -37,7 +56,13 @@ public class Document implements DocumentInterface {
|
|
37
|
56
|
|
|
38
|
57
|
@Override
|
|
39
|
58
|
public String read() {
|
|
40
|
|
- return null;
|
|
|
59
|
+ String result = "";
|
|
|
60
|
+ try {
|
|
|
61
|
+ result = new String(Files.readAllBytes(Paths.get(file.getPath())));
|
|
|
62
|
+ } catch (IOException e) {
|
|
|
63
|
+ e.printStackTrace();
|
|
|
64
|
+ }
|
|
|
65
|
+ return result;
|
|
41
|
66
|
}
|
|
42
|
67
|
|
|
43
|
68
|
@Override
|
|
|
@@ -49,7 +74,14 @@ public class Document implements DocumentInterface {
|
|
49
|
74
|
}
|
|
50
|
75
|
|
|
51
|
76
|
public List<String> toList() {
|
|
52
|
|
- return null;
|
|
|
77
|
+ List<String> lines = new ArrayList() {
|
|
|
78
|
+ };
|
|
|
79
|
+ try {
|
|
|
80
|
+ lines = Files.readAllLines(file.toPath());
|
|
|
81
|
+ } catch (IOException e) {
|
|
|
82
|
+ e.printStackTrace();
|
|
|
83
|
+ }
|
|
|
84
|
+ return lines;
|
|
53
|
85
|
}
|
|
54
|
86
|
|
|
55
|
87
|
@Override
|
|
|
@@ -61,4 +93,4 @@ public class Document implements DocumentInterface {
|
|
61
|
93
|
public String toString() {
|
|
62
|
94
|
return null;
|
|
63
|
95
|
}
|
|
64
|
|
-}
|
|
|
96
|
+}
|