mpierse 7 лет назад
Родитель
Сommit
5a0617111b
1 измененных файлов: 31 добавлений и 14 удалений
  1. 31
    14
      src/main/java/rocks/zipcode/Document.java

+ 31
- 14
src/main/java/rocks/zipcode/Document.java Просмотреть файл

@@ -2,16 +2,16 @@ package rocks.zipcode;
2 2
 
3 3
 import java.io.*;
4 4
 import java.nio.file.Files;
5
+import java.util.ArrayList;
5 6
 import java.util.Arrays;
6 7
 import java.util.List;
7
-import java.util.Scanner;
8 8
 
9 9
 /**
10 10
  * @author leon on 16/11/2018.
11 11
  */
12 12
 public class Document implements DocumentInterface {
13 13
 
14
-    private final FileWriter fw;
14
+    private final Writer bw;
15 15
     private final BufferedReader br;
16 16
     private final File file;
17 17
 
@@ -21,15 +21,15 @@ public class Document implements DocumentInterface {
21 21
 
22 22
     public Document(File file) throws IOException {
23 23
         this.file = file;
24
-        this.fw = new FileWriter(file);
24
+        this.bw = new FileWriter(file,false);
25 25
         this.br = new BufferedReader(new FileReader(file));
26 26
     }
27 27
 
28 28
     @Override
29 29
     public void write(String contentToBeWritten) {
30 30
         try {
31
-            fw.write(contentToBeWritten);
32
-            fw.flush();
31
+            bw.write(contentToBeWritten);
32
+            bw.flush();
33 33
         } catch (IOException ex) {
34 34
             ex.printStackTrace();
35 35
         }
@@ -37,7 +37,19 @@ public class Document implements DocumentInterface {
37 37
 
38 38
     @Override
39 39
     public void write(Integer lineNumber, String valueToBeWritten) {
40
-
40
+//        try {
41
+//            String result = "";
42
+//            String[] input = this.read().split("\n");
43
+//            input[lineNumber]=valueToBeWritten;
44
+//            for (String str: input) {
45
+//                result += str;
46
+//                result += "\n";
47
+//            }
48
+//            bw.write(result);
49
+//            bw.flush();
50
+//        } catch (IOException ex) {
51
+//            ex.printStackTrace();
52
+//        }
41 53
     }
42 54
 
43 55
     @Override
@@ -46,16 +58,15 @@ public class Document implements DocumentInterface {
46 58
     }
47 59
 
48 60
     @Override
49
-    public String read() throws FileNotFoundException {
50
-        String result="";
61
+    public String read() {
62
+        byte[] result = null;
51 63
         try{
52 64
             String st;
53
-            while ((st = br.readLine()) != null)
54
-                result += st;}
65
+          result =Files.readAllBytes(file.toPath());}
55 66
         catch (IOException e){
56 67
             System.out.println("File not found for buffered reader.");
57 68
         }
58
-        return result;
69
+        return new String(result);
59 70
     }
60 71
 
61 72
     @Override
@@ -64,19 +75,25 @@ public class Document implements DocumentInterface {
64 75
 
65 76
     @Override
66 77
     public void overWrite(String content) {
78
+        try {
79
+            bw.write(content);
80
+            bw.flush();
81
+        } catch (IOException e) {
82
+            e.printStackTrace();
83
+        }
67 84
     }
68 85
 
69 86
     public List<String> toList() {
70
-        return null;
87
+       return new ArrayList<String>(Arrays.asList(this.read().split("\n")));
71 88
     }
72 89
 
73 90
     @Override
74 91
     public File getFile() {
75
-        return null;
92
+       return this.file;
76 93
     }
77 94
 
78 95
     @Override
79 96
     public String toString() {
80
-        return null;
97
+        return this.getFile() + "{" + this.read() +"}";
81 98
     }
82 99
 }