|
@@ -1,5 +1,4 @@
|
1
|
|
-import java.io.File;
|
2
|
|
-import java.io.IOException;
|
|
1
|
+import java.io.*;
|
3
|
2
|
import java.util.Scanner;
|
4
|
3
|
|
5
|
4
|
/**
|
|
@@ -16,15 +15,13 @@ public class HamletParser {
|
16
|
15
|
private String loadFile(){
|
17
|
16
|
ClassLoader classLoader = getClass().getClassLoader();
|
18
|
17
|
File file = new File(classLoader.getResource("hamlet.txt").getFile());
|
19
|
|
- StringBuilder result = new StringBuilder("");
|
|
18
|
+ StringBuilder result = new StringBuilder();
|
20
|
19
|
|
21
|
20
|
try(Scanner scanner = new Scanner(file)){
|
22
|
21
|
while(scanner.hasNextLine()){
|
23
|
22
|
String line = scanner.nextLine();
|
24
|
23
|
result.append(line).append("\n");
|
25
|
24
|
}
|
26
|
|
-
|
27
|
|
- scanner.close();
|
28
|
25
|
}catch(IOException e){
|
29
|
26
|
e.printStackTrace();
|
30
|
27
|
}
|
|
@@ -36,4 +33,48 @@ public class HamletParser {
|
36
|
33
|
return hamletData;
|
37
|
34
|
}
|
38
|
35
|
|
|
36
|
+
|
|
37
|
+ public String modifyFile(String filepath, String oldString, String newString){
|
|
38
|
+ ClassLoader classLoader = getClass().getClassLoader();
|
|
39
|
+
|
|
40
|
+ File fileToBeModified = new File(classLoader.getResource("hamlet.txt").getFile());
|
|
41
|
+
|
|
42
|
+ String oldContent = "";
|
|
43
|
+ BufferedReader reader = null;
|
|
44
|
+ FileWriter writer = null;
|
|
45
|
+
|
|
46
|
+ try{
|
|
47
|
+ reader = new BufferedReader(new FileReader(fileToBeModified));
|
|
48
|
+
|
|
49
|
+ String line = reader.readLine();
|
|
50
|
+ while(line != null)
|
|
51
|
+ {
|
|
52
|
+ oldContent = oldContent + line + System.lineSeparator();
|
|
53
|
+
|
|
54
|
+ line = reader.readLine();
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ String newContent = oldContent.replaceAll(oldString,newString);
|
|
58
|
+
|
|
59
|
+ writer = new FileWriter(fileToBeModified);
|
|
60
|
+ writer.write(newContent);
|
|
61
|
+ }
|
|
62
|
+ catch(IOException e)
|
|
63
|
+ {
|
|
64
|
+ e.printStackTrace();
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ return filepath;
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+// public String modifiedFile(){}
|
|
71
|
+
|
|
72
|
+ public static void main(String[] args) {
|
|
73
|
+ HamletParser hamletParser = new HamletParser();
|
|
74
|
+
|
|
75
|
+ String hamletFile = hamletParser.loadFile();
|
|
76
|
+
|
|
77
|
+ System.out.println(hamletParser.hamletData);
|
|
78
|
+
|
|
79
|
+ }
|
39
|
80
|
}
|