Elliott Stansbury 6 年前
父节点
当前提交
8750c736a2
共有 4 个文件被更改,包括 4348 次插入5 次删除
  1. 12
    0
      pom.xml
  2. 46
    5
      src/main/java/HamletParser.java
  3. 21
    0
      src/test/java/HamletParserTest.java
  4. 4269
    0
      target/classes/hamlet.txt

+ 12
- 0
pom.xml 查看文件

7
     <groupId>com.zipcodewilmington</groupId>
7
     <groupId>com.zipcodewilmington</groupId>
8
     <artifactId>regex</artifactId>
8
     <artifactId>regex</artifactId>
9
     <version>1.0-SNAPSHOT</version>
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
     <dependencies>
23
     <dependencies>
12
         <dependency>
24
         <dependency>

+ 46
- 5
src/main/java/HamletParser.java 查看文件

1
-import java.io.File;
2
-import java.io.IOException;
1
+import java.io.*;
3
 import java.util.Scanner;
2
 import java.util.Scanner;
4
 
3
 
5
 /**
4
 /**
16
     private String loadFile(){
15
     private String loadFile(){
17
         ClassLoader classLoader = getClass().getClassLoader();
16
         ClassLoader classLoader = getClass().getClassLoader();
18
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
17
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19
-        StringBuilder result = new StringBuilder("");
18
+        StringBuilder result = new StringBuilder();
20
 
19
 
21
         try(Scanner scanner = new Scanner(file)){
20
         try(Scanner scanner = new Scanner(file)){
22
             while(scanner.hasNextLine()){
21
             while(scanner.hasNextLine()){
23
                 String line = scanner.nextLine();
22
                 String line = scanner.nextLine();
24
                 result.append(line).append("\n");
23
                 result.append(line).append("\n");
25
             }
24
             }
26
-
27
-            scanner.close();
28
         }catch(IOException e){
25
         }catch(IOException e){
29
             e.printStackTrace();
26
             e.printStackTrace();
30
         }
27
         }
36
         return hamletData;
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 查看文件

15
 
15
 
16
     @Test
16
     @Test
17
     public void testChangeHamletToLeon() {
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
     @Test
26
     @Test
21
     public void testChangeHoratioToTariq() {
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
     @Test
35
     @Test
25
     public void testFindHoratio() {
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
     @Test
44
     @Test
29
     public void testFindHamlet() {
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
文件差异内容过多而无法显示
查看文件