Selaa lähdekoodia

refactored alpha and started document

mpierse 7 vuotta sitten
vanhempi
commit
92be52e1d7

+ 5
- 26
src/main/java/rocks/zipcode/AlphaCharDocument.java Näytä tiedosto

@@ -9,45 +9,24 @@ import java.util.Scanner;
9 9
 public class AlphaCharDocument extends Document {
10 10
 
11 11
 
12
-private File file;
13
-
14 12
 
15 13
     public AlphaCharDocument(String fileName) throws IOException {
16 14
         super(fileName);
17
-        this.file = new File(fileName);
18
-       // this.fileName = fileName;
19
-       // fw = new FileWriter(file);
20 15
     }
21 16
 
22 17
     @Override
23 18
     public void write(String contentToBeWritten) {
24 19
         try {
25
-            FileWriter fw = new FileWriter(file);
26 20
             isAlpha(contentToBeWritten);
27
-            fw.write(contentToBeWritten);
28
-            fw.close();
29
-        } catch (IOException ex) {
30
-            ex.printStackTrace();
31
-        }
32
-    }
33
-
34
-    public String read() throws FileNotFoundException {
35
-        try {
36
-            Scanner scanner = new Scanner(file);
37
-            StringBuilder sb = new StringBuilder();
38
-            while(scanner.hasNextLine()){
39
-                sb.append(scanner.nextLine());
40
-            }
41
-            String content = sb.toString();
42
-            return content;
43
-
44
-    } catch (IOException e) {
21
+            super.write(contentToBeWritten);
22
+        } catch (IOException e) {
45 23
             e.printStackTrace();
46 24
         }
47
-        return null;
48 25
     }
49 26
 
50
-    private void isAlpha(String s) throws IOException {
27
+
28
+
29
+    private void isAlpha(String s) throws IOException{
51 30
         if(!s.matches("[a-zA-Z\\s]*"))
52 31
             throw new IllegalArgumentException();
53 32
     }

+ 14
- 15
src/main/java/rocks/zipcode/Document.java Näytä tiedosto

@@ -12,22 +12,24 @@ import java.util.Scanner;
12 12
 public class Document implements DocumentInterface {
13 13
 
14 14
     private final FileWriter fw;
15
-   // private final File file;
15
+    private final BufferedReader br;
16
+    private final File file;
16 17
 
17 18
     public Document(String fileName) throws IOException {
18 19
         this(new File(fileName));
19 20
     }
20 21
 
21 22
     public Document(File file) throws IOException {
22
-      //  this.file = file;
23
+        this.file = file;
23 24
         this.fw = new FileWriter(file);
25
+        this.br = new BufferedReader(new FileReader(file));
24 26
     }
25 27
 
26 28
     @Override
27 29
     public void write(String contentToBeWritten) {
28 30
         try {
29 31
             fw.write(contentToBeWritten);
30
-            fw.close();
32
+            fw.flush();
31 33
         } catch (IOException ex) {
32 34
             ex.printStackTrace();
33 35
         }
@@ -35,17 +37,6 @@ public class Document implements DocumentInterface {
35 37
 
36 38
     @Override
37 39
     public void write(Integer lineNumber, String valueToBeWritten) {
38
-//        Scanner scanner = null;
39
-//        try {
40
-//            scanner = new Scanner(file);
41
-//        } catch (FileNotFoundException e) {
42
-//            e.printStackTrace();
43
-//        }
44
-//        StringBuilder sb = new StringBuilder();
45
-//        while(scanner.hasNextLine()){
46
-//            sb.append(scanner.nextLine());
47
-//        }
48
-//        String content = sb.toString();
49 40
 
50 41
     }
51 42
 
@@ -56,7 +47,15 @@ public class Document implements DocumentInterface {
56 47
 
57 48
     @Override
58 49
     public String read() throws FileNotFoundException {
59
-        return null;
50
+        String result="";
51
+        try{
52
+            String st;
53
+            while ((st = br.readLine()) != null)
54
+                result += st;}
55
+        catch (IOException e){
56
+            System.out.println("File not found for buffered reader.");
57
+        }
58
+        return result;
60 59
     }
61 60
 
62 61
     @Override