|
|
@@ -1,9 +1,14 @@
|
|
1
|
1
|
package rocks.zipcode;
|
|
2
|
2
|
|
|
3
|
3
|
import java.io.*;
|
|
|
4
|
+import java.nio.charset.Charset;
|
|
|
5
|
+import java.nio.charset.StandardCharsets;
|
|
4
|
6
|
import java.nio.file.Files;
|
|
|
7
|
+import java.nio.file.Path;
|
|
|
8
|
+import java.nio.file.Paths;
|
|
5
|
9
|
import java.util.Arrays;
|
|
6
|
10
|
import java.util.List;
|
|
|
11
|
+import java.util.stream.Stream;
|
|
7
|
12
|
|
|
8
|
13
|
/**
|
|
9
|
14
|
* @author leon on 16/11/2018.
|
|
|
@@ -13,6 +18,8 @@ public class Document implements DocumentInterface {
|
|
13
|
18
|
private final FileWriter fileWriter;
|
|
14
|
19
|
private final File file;
|
|
15
|
20
|
|
|
|
21
|
+
|
|
|
22
|
+
|
|
16
|
23
|
public Document(String fileName) throws IOException {
|
|
17
|
24
|
this(new File(fileName));
|
|
18
|
25
|
}
|
|
|
@@ -22,17 +29,35 @@ public class Document implements DocumentInterface {
|
|
22
|
29
|
this.fileWriter = new FileWriter(file);
|
|
23
|
30
|
}
|
|
24
|
31
|
|
|
|
32
|
+
|
|
25
|
33
|
@Override
|
|
26
|
|
- public void write(String contentToBeWritten) {
|
|
|
34
|
+ public void write(String contentToBeWritten) throws IOException{
|
|
|
35
|
+ BufferedWriter writer = new BufferedWriter(new FileWriter(file));
|
|
|
36
|
+ writer.write(contentToBeWritten);
|
|
|
37
|
+ writer.close();
|
|
27
|
38
|
}
|
|
28
|
39
|
|
|
29
|
40
|
@Override
|
|
30
|
|
- public void write(Integer lineNumber, String valueToBeWritten) {
|
|
|
41
|
+ public void write(Integer lineNumber, String valueToBeWritten) throws IOException {
|
|
|
42
|
+ List <String> lines = null;
|
|
|
43
|
+ try {
|
|
|
44
|
+ lines = Files.readAllLines(file.toPath());
|
|
|
45
|
+ lines.set(lineNumber,valueToBeWritten);
|
|
|
46
|
+ Files.write(file.toPath(), lines);
|
|
|
47
|
+ } catch (IOException e) {
|
|
|
48
|
+ e.printStackTrace();
|
|
|
49
|
+ }
|
|
|
50
|
+
|
|
31
|
51
|
}
|
|
32
|
52
|
|
|
33
|
53
|
@Override
|
|
34
|
|
- public String read(Integer lineNumber) {
|
|
35
|
|
- return null;
|
|
|
54
|
+ public String read(Integer lineNumber) throws IOException {
|
|
|
55
|
+
|
|
|
56
|
+ try(Stream<String> lines = Files.lines(Paths.get("target/file.txt"))){
|
|
|
57
|
+ String line = lines.skip(lineNumber).findFirst().get();
|
|
|
58
|
+ return line;
|
|
|
59
|
+ }
|
|
|
60
|
+
|
|
36
|
61
|
}
|
|
37
|
62
|
|
|
38
|
63
|
@Override
|
|
|
@@ -51,6 +76,21 @@ public class Document implements DocumentInterface {
|
|
51
|
76
|
|
|
52
|
77
|
@Override
|
|
53
|
78
|
public void replaceAll(String stringToReplace, String replacementString) {
|
|
|
79
|
+ Path path = Paths.get("target/file.txt");
|
|
|
80
|
+ Charset charset = StandardCharsets.UTF_8;
|
|
|
81
|
+
|
|
|
82
|
+ String content = null;
|
|
|
83
|
+ try {
|
|
|
84
|
+ content = new String(Files.readAllBytes(path), charset);
|
|
|
85
|
+ } catch (IOException e) {
|
|
|
86
|
+ e.printStackTrace();
|
|
|
87
|
+ }
|
|
|
88
|
+ content = content.replaceAll(stringToReplace, replacementString);
|
|
|
89
|
+ try {
|
|
|
90
|
+ Files.write(path, content.getBytes(charset));
|
|
|
91
|
+ } catch (IOException e) {
|
|
|
92
|
+ e.printStackTrace();
|
|
|
93
|
+ }
|
|
54
|
94
|
}
|
|
55
|
95
|
|
|
56
|
96
|
@Override
|