Sfoglia il codice sorgente

finished read and write in document class

Whitney Martinez 7 anni fa
parent
commit
41fe57f3be

+ 15
- 2
src/main/java/rocks/zipcode/AlphaCharDocument.java Vedi File

@@ -1,21 +1,34 @@
1 1
 package rocks.zipcode;
2 2
 
3
+import java.io.BufferedWriter;
4
+import java.io.FileWriter;
3 5
 import java.io.IOException;
4 6
 
5 7
 /**
6 8
  * @author leon on 16/11/2018.
7 9
  */
8 10
 public class AlphaCharDocument extends Document {
11
+    Document document;
12
+
9 13
     public AlphaCharDocument(String fileName) throws IOException {
14
+
10 15
         super(fileName);
16
+
11 17
     }
12 18
 
13 19
     @Override
14
-    public void write(String contentToBeWritten) {
20
+    public void write(String contentToBeWritten) throws IOException {
21
+
22
+     
23
+
15 24
     }
16 25
 
17 26
     private Boolean isAlpha(String s) {
18 27
 
19
-        return null;
28
+        boolean allLetters = s.chars().allMatch(Character::isLetter);
29
+        return allLetters;
30
+
20 31
     }
32
+
33
+
21 34
 }

+ 44
- 4
src/main/java/rocks/zipcode/Document.java Vedi File

@@ -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

+ 4
- 3
src/main/java/rocks/zipcode/DocumentInterface.java Vedi File

@@ -1,17 +1,18 @@
1 1
 package rocks.zipcode;
2 2
 
3 3
 import java.io.File;
4
+import java.io.IOException;
4 5
 import java.util.List;
5 6
 
6 7
 /**
7 8
  * @author leon on 16/11/2018.
8 9
  */
9 10
 public interface DocumentInterface {
10
-    void write(String contentToBeWritten);
11
+    void write(String contentToBeWritten) throws IOException;
11 12
 
12
-    void write(Integer lineNumber, String valueToBeWritten);
13
+    void write(Integer lineNumber, String valueToBeWritten) throws IOException;
13 14
 
14
-    String read(Integer lineNumber);
15
+    String read(Integer lineNumber) throws IOException;
15 16
 
16 17
     String read();
17 18