|
@@ -2,8 +2,16 @@ package rocks.zipcode;
|
2
|
2
|
|
3
|
3
|
import java.io.*;
|
4
|
4
|
import java.nio.file.Files;
|
|
5
|
+import java.nio.file.Path;
|
|
6
|
+import java.nio.file.Paths;
|
|
7
|
+import java.util.ArrayList;
|
5
|
8
|
import java.util.Arrays;
|
6
|
9
|
import java.util.List;
|
|
10
|
+import java.util.Scanner;
|
|
11
|
+import java.util.regex.Matcher;
|
|
12
|
+import java.util.regex.Pattern;
|
|
13
|
+import java.util.stream.Collectors;
|
|
14
|
+import java.util.stream.Stream;
|
7
|
15
|
|
8
|
16
|
/**
|
9
|
17
|
* @author leon on 16/11/2018.
|
|
@@ -24,41 +32,158 @@ 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
|
+ //fileWriter.close();
|
|
39
|
+ } catch (IOException e) {
|
|
40
|
+ e.printStackTrace();
|
|
41
|
+ } finally {
|
|
42
|
+
|
|
43
|
+ }
|
|
44
|
+
|
27
|
45
|
}
|
28
|
46
|
|
29
|
47
|
@Override
|
30
|
48
|
public void write(Integer lineNumber, String valueToBeWritten) {
|
|
49
|
+//
|
|
50
|
+ try {
|
|
51
|
+ StringBuilder sb=new StringBuilder() ;
|
|
52
|
+ try {
|
|
53
|
+ BufferedReader fileReader =new BufferedReader( new FileReader( file)) ;
|
|
54
|
+ String fileread ="";
|
|
55
|
+ int i=0;
|
|
56
|
+ while((fileread =fileReader.readLine()) !=null){
|
|
57
|
+ if(lineNumber==i) {
|
|
58
|
+ sb.append(valueToBeWritten);
|
|
59
|
+ } else {
|
|
60
|
+ sb.append(fileread);
|
|
61
|
+ }
|
|
62
|
+ sb.append("\n");
|
|
63
|
+ i++;
|
|
64
|
+ }
|
|
65
|
+ overWrite(sb.toString());
|
|
66
|
+ }catch (Exception e) {
|
|
67
|
+ e.printStackTrace();
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+ } catch (Exception e) {
|
|
74
|
+ e.printStackTrace();
|
|
75
|
+ }
|
31
|
76
|
}
|
32
|
77
|
|
33
|
78
|
@Override
|
34
|
79
|
public String read(Integer lineNumber) {
|
35
|
|
- return null;
|
|
80
|
+ try {
|
|
81
|
+ //return new String(Files.readAllBytes(Paths.get(file.getPath())));
|
|
82
|
+ String str = new String(Files.readAllLines(Paths.get(file.getPath())).get(lineNumber));
|
|
83
|
+ return str;
|
|
84
|
+ } catch (Exception e) {
|
|
85
|
+ e.printStackTrace();
|
|
86
|
+ return null;
|
|
87
|
+ }
|
|
88
|
+
|
36
|
89
|
}
|
37
|
90
|
|
38
|
91
|
@Override
|
39
|
92
|
public String read() {
|
40
|
|
- return null;
|
|
93
|
+
|
|
94
|
+ StringBuilder sb=new StringBuilder() ;
|
|
95
|
+ try {
|
|
96
|
+ BufferedReader fileReader =new BufferedReader( new FileReader( file)) ;
|
|
97
|
+ String fileread ="";
|
|
98
|
+ while((fileread =fileReader.readLine()) !=null){
|
|
99
|
+ sb.append(fileread);
|
|
100
|
+ sb.append("\n") ;
|
|
101
|
+
|
|
102
|
+ } }catch (Exception e) {
|
|
103
|
+ e.printStackTrace();
|
|
104
|
+ }
|
|
105
|
+ return sb.toString().trim() ;
|
|
106
|
+// try {
|
|
107
|
+// return new String(Files.readAllBytes(Paths.get(file.getPath())));
|
|
108
|
+// } catch (Exception e) {
|
|
109
|
+// e.printStackTrace();
|
|
110
|
+// return null;
|
|
111
|
+// }
|
|
112
|
+
|
41
|
113
|
}
|
42
|
114
|
|
|
115
|
+
|
43
|
116
|
@Override
|
44
|
|
- public void replaceAll(String stringToReplace, String replacementString) {
|
45
|
|
- }
|
|
117
|
+ public void replaceAll(String stringToReplace, String replacementString) { //NOT WORKING
|
|
118
|
+ try{
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+ String strFileInputs = read();
|
|
122
|
+ System.out.println("Inside replaceAll method - After reading the input - " + strFileInputs );
|
|
123
|
+ Pattern ptn = Pattern.compile(stringToReplace );
|
|
124
|
+ Matcher mtch = ptn.matcher(strFileInputs );
|
|
125
|
+ String replaced=mtch.replaceAll(replacementString );
|
46
|
126
|
|
|
127
|
+ overWrite(replaced ) ;
|
|
128
|
+
|
|
129
|
+ }catch (Exception e ){
|
|
130
|
+ e.printStackTrace() ;
|
|
131
|
+ }
|
|
132
|
+
|
|
133
|
+ }
|
47
|
134
|
@Override
|
48
|
135
|
public void overWrite(String content) {
|
|
136
|
+
|
|
137
|
+ try {
|
|
138
|
+
|
|
139
|
+ FileWriter fstream = new FileWriter(file, false);
|
|
140
|
+ fstream.write(content);
|
|
141
|
+ fstream.flush();
|
|
142
|
+ } catch (Exception e) {
|
|
143
|
+ e.printStackTrace();
|
|
144
|
+ }
|
|
145
|
+
|
49
|
146
|
}
|
50
|
147
|
|
51
|
148
|
public List<String> toList() {
|
52
|
|
- return null;
|
|
149
|
+
|
|
150
|
+ List<String> list = null;
|
|
151
|
+ try (Stream<String> lines = Files.lines(Paths.get(file.getPath()))) {
|
|
152
|
+ list = lines.collect(Collectors.toList());
|
|
153
|
+ } catch (IOException e) {
|
|
154
|
+ e.printStackTrace();
|
|
155
|
+ }
|
|
156
|
+ return list;
|
53
|
157
|
}
|
54
|
158
|
|
55
|
159
|
@Override
|
56
|
160
|
public File getFile() {
|
57
|
|
- return null;
|
|
161
|
+ try {
|
|
162
|
+ String str = "";
|
|
163
|
+ ClassLoader cl = getClass().getClassLoader();
|
|
164
|
+ File file = new File(cl.getResource(str).getFile());
|
|
165
|
+ return file;
|
|
166
|
+ } catch (Exception e) {
|
|
167
|
+ e.printStackTrace();
|
|
168
|
+ return null;
|
|
169
|
+ }
|
58
|
170
|
}
|
59
|
171
|
|
60
|
172
|
@Override
|
61
|
173
|
public String toString() {
|
62
|
|
- return null;
|
|
174
|
+ try {
|
|
175
|
+ StringBuilder sb=new StringBuilder() ;
|
|
176
|
+ String filepath=file.toString() ;
|
|
177
|
+ String str= new String(Files.readAllBytes(Paths.get(file.getPath())));
|
|
178
|
+ sb.append(filepath ) ;
|
|
179
|
+ sb.append( "{");
|
|
180
|
+ sb.append(str) ;
|
|
181
|
+ sb.append("}");
|
|
182
|
+
|
|
183
|
+ return sb.toString() ;
|
|
184
|
+ } catch (Exception e) {
|
|
185
|
+ e.printStackTrace();
|
|
186
|
+ return null;
|
|
187
|
+ }
|
63
|
188
|
}
|
64
|
189
|
}
|