Elliott Stansbury 6 lat temu
rodzic
commit
8750c736a2

+ 12
- 0
pom.xml Wyświetl plik

@@ -7,6 +7,18 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>regex</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>7</source>
17
+                    <target>7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

+ 46
- 5
src/main/java/HamletParser.java Wyświetl plik

@@ -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
 }

+ 21
- 0
src/test/java/HamletParserTest.java Wyświetl plik

@@ -15,17 +15,38 @@ public class HamletParserTest {
15 15
 
16 16
     @Test
17 17
     public void testChangeHamletToLeon() {
18
+
19
+        hamletParser.getHamletData();
20
+        String expected = hamletParser.modifyFile(hamletText,"Hamlet","Leon");
21
+
22
+        System.out.println(expected);
23
+
18 24
     }
19 25
 
20 26
     @Test
21 27
     public void testChangeHoratioToTariq() {
28
+
29
+        hamletParser.getHamletData();
30
+        String expected = hamletParser.modifyFile(hamletText,"Horatio","Tariq");
31
+
32
+        System.out.println(expected);
22 33
     }
23 34
 
24 35
     @Test
25 36
     public void testFindHoratio() {
37
+
38
+        hamletParser.getHamletData();
39
+        String expected = hamletParser.modifyFile(hamletText,"Tariq","Horatio");
40
+
41
+        System.out.println(expected);
26 42
     }
27 43
 
28 44
     @Test
29 45
     public void testFindHamlet() {
46
+
47
+        hamletParser.getHamletData();
48
+        String expected = hamletParser.modifyFile(hamletText,"Leon","Hamlet");
49
+
50
+        System.out.println(expected);
30 51
     }
31 52
 }

+ 4269
- 0
target/classes/hamlet.txt
Plik diff jest za duży
Wyświetl plik