瀏覽代碼

readAndWrite

Zavon Malone 7 年之前
父節點
當前提交
64f3e21d60

+ 8
- 0
pom.xml 查看文件

@@ -20,6 +20,14 @@
20 20
         </plugins>
21 21
     </build>
22 22
     <dependencies>
23
+        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
24
+        <dependency>
25
+            <groupId>org.apache.commons</groupId>
26
+            <artifactId>commons-io</artifactId>
27
+            <version>1.3.2</version>
28
+        </dependency>
29
+
30
+
23 31
         <dependency>
24 32
             <groupId>junit</groupId>
25 33
             <artifactId>junit</artifactId>

+ 19
- 1
src/main/java/rocks/zipcode/AlphaCharDocument.java 查看文件

@@ -1,6 +1,11 @@
1 1
 package rocks.zipcode;
2 2
 
3
+import com.sun.deploy.util.StringUtils;
4
+
5
+import java.io.FileWriter;
3 6
 import java.io.IOException;
7
+import java.util.regex.Matcher;
8
+import java.util.regex.Pattern;
4 9
 
5 10
 /**
6 11
  * @author leon on 16/11/2018.
@@ -12,9 +17,22 @@ public class AlphaCharDocument extends Document {
12 17
 
13 18
     @Override
14 19
     public void write(String contentToBeWritten) {
20
+        if(isAlpha(contentToBeWritten)){
21
+        try {
22
+            FileWriter fileWriter = new FileWriter("target/file.txt");
23
+            fileWriter.write(contentToBeWritten);
24
+            fileWriter.flush();
25
+        } catch (IOException e){
26
+            e.printStackTrace();
27
+        }
28
+        }else{
29
+            throw new IllegalArgumentException();
30
+        }
15 31
     }
16 32
 
17 33
     private Boolean isAlpha(String s) {
18
-        return null;
34
+        Pattern pattern = Pattern.compile("[a-zA-Z]");
35
+        Matcher matcher = pattern.matcher(s);
36
+        return matcher.find();
19 37
     }
20 38
 }

+ 54
- 4
src/main/java/rocks/zipcode/Document.java 查看文件

@@ -1,9 +1,17 @@
1 1
 package rocks.zipcode;
2 2
 
3
+import org.apache.commons.io.FileUtils;
4
+
3 5
 import java.io.*;
6
+import java.net.URI;
7
+import java.nio.charset.StandardCharsets;
4 8
 import java.nio.file.Files;
9
+import java.nio.file.Path;
10
+import java.nio.file.Paths;
11
+import java.nio.file.StandardOpenOption;
5 12
 import java.util.Arrays;
6 13
 import java.util.List;
14
+import java.util.stream.Stream;
7 15
 
8 16
 /**
9 17
  * @author leon on 16/11/2018.
@@ -24,28 +32,64 @@ public class Document implements DocumentInterface {
24 32
 
25 33
     @Override
26 34
     public void write(String contentToBeWritten) {
35
+        try {
36
+            fileWriter.write(contentToBeWritten);
37
+            fileWriter.flush();
38
+        } catch (IOException e){
39
+            e.printStackTrace();
40
+        }
27 41
     }
28 42
 
29 43
     @Override
30 44
     public void write(Integer lineNumber, String valueToBeWritten) {
45
+        try {
46
+            Path path = Paths.get("target/file.txt");
47
+            List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
48
+            lines.set(lineNumber , valueToBeWritten);
49
+            Files.write(path, lines.subList(0, lines.size()-1), StandardCharsets.UTF_8);
50
+            Files.write(path, lines.get(lines.size()-1).getBytes("UTF-8"), StandardOpenOption.APPEND);
51
+        } catch (IOException e){
52
+            e.printStackTrace();
53
+        }
31 54
     }
32 55
 
33 56
     @Override
34
-    public String read(Integer lineNumber) {
35
-        return null;
57
+    public String read(Integer lineNumber) throws IOException {
58
+        return Files.readAllLines(file.toPath()).get(lineNumber);
36 59
     }
37 60
 
38 61
     @Override
39
-    public String read() {
40
-        return null;
62
+    public String read() throws IOException{
63
+
64
+            return new String(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
65
+
41 66
     }
42 67
 
43 68
     @Override
44 69
     public void replaceAll(String stringToReplace, String replacementString) {
70
+        try {
71
+            Path path = Paths.get("/Users/zavonmalone/labs/exceptions-readandwrite/target/file.txt");
72
+            String content = new String(Files.readAllBytes(path),StandardCharsets.UTF_8);
73
+            content = content.replaceAll(stringToReplace, replacementString);
74
+            Files.write(path, content.getBytes(StandardCharsets.UTF_8));
75
+
76
+        }catch(IOException e){
77
+            e.printStackTrace();
78
+        }
79
+
45 80
     }
46 81
 
47 82
     @Override
48 83
     public void overWrite(String content) {
84
+        try {
85
+            Path path = Paths.get("target/file.txt");
86
+            List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
87
+            lines.clear();
88
+            lines.add(content);
89
+            Files.write(path, lines.subList(0, lines.size()-1), StandardCharsets.UTF_8);
90
+            Files.write(path, lines.get(lines.size()-1).getBytes("UTF-8"), StandardOpenOption.APPEND);        } catch (IOException e) {
91
+            e.printStackTrace();
92
+        }
49 93
     }
50 94
 
51 95
     public List<String> toList() {
@@ -54,11 +98,17 @@ public class Document implements DocumentInterface {
54 98
 
55 99
     @Override
56 100
     public File getFile() {
101
+        Path path = Paths.get("/Users/zavonmalone/labs/exceptions-readandwrite/target/file.txt");
57 102
         return null;
58 103
     }
59 104
 
60 105
     @Override
61 106
     public String toString() {
107
+        try {
108
+            return file.toString() + "{" +read() + "}";
109
+        }catch(IOException e){
110
+            e.printStackTrace();
111
+        }
62 112
         return null;
63 113
     }
64 114
 }

+ 4
- 2
src/main/java/rocks/zipcode/DocumentInterface.java 查看文件

@@ -1,6 +1,8 @@
1 1
 package rocks.zipcode;
2 2
 
3 3
 import java.io.File;
4
+import java.io.FileNotFoundException;
5
+import java.io.IOException;
4 6
 import java.util.List;
5 7
 
6 8
 /**
@@ -11,9 +13,9 @@ public interface DocumentInterface {
11 13
 
12 14
     void write(Integer lineNumber, String valueToBeWritten);
13 15
 
14
-    String read(Integer lineNumber);
16
+    String read(Integer lineNumber) throws FileNotFoundException, IOException;
15 17
 
16
-    String read();
18
+    String read() throws IOException;
17 19
 
18 20
     void replaceAll(String stringToReplace, String replacementString);
19 21
 

+ 10
- 1
src/main/java/rocks/zipcode/NumericCharDocument.java 查看文件

@@ -1,6 +1,8 @@
1 1
 package rocks.zipcode;
2 2
 
3 3
 import java.io.IOException;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
4 6
 
5 7
 /**
6 8
  * @author leon on 16/11/2018.
@@ -12,9 +14,16 @@ public class NumericCharDocument extends Document {
12 14
 
13 15
     @Override
14 16
     public void write(String contentToBeWritten) {
17
+        if(isNumeric(contentToBeWritten)){
18
+            super.write(contentToBeWritten);
19
+        }else{
20
+            throw new IllegalArgumentException();
21
+        }
15 22
     }
16 23
 
17 24
     private Boolean isNumeric(String s) {
18
-        return null;
25
+        Pattern pattern = Pattern.compile("[0-9]");
26
+        Matcher matcher = pattern.matcher(s);
27
+        return matcher.find();
19 28
     }
20 29
 }

+ 10
- 1
src/main/java/rocks/zipcode/SpecialCharDocument.java 查看文件

@@ -1,6 +1,8 @@
1 1
 package rocks.zipcode;
2 2
 
3 3
 import java.io.IOException;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
4 6
 
5 7
 /**
6 8
  * @author leon on 18/11/2018.
@@ -12,9 +14,16 @@ public class SpecialCharDocument extends Document {
12 14
 
13 15
     @Override
14 16
     public void write(String contentToBeWritten) {
17
+    if(isSpecialCharacters(contentToBeWritten)){
18
+        super.write(contentToBeWritten);
19
+    }else{
20
+        throw new IllegalArgumentException();
21
+    }
15 22
     }
16 23
 
17 24
     private Boolean isSpecialCharacters(String s) {
18
-        return null;
25
+        Pattern pattern = Pattern.compile("[^\\w\\s]");
26
+        Matcher matcher = pattern.matcher(s);
27
+        return matcher.find();
19 28
     }
20 29
 }