-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReplace.java
More file actions
77 lines (73 loc) · 2.61 KB
/
FileReplace.java
File metadata and controls
77 lines (73 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by Shike Feng on 17-Dec-15.
*/
public class FileReplace {
ArrayList<String> lines;
String filename;
String line;
HashMap<String, String> match_table;
public FileReplace(String filename, HashMap<String,String> match_table){
this.filename = filename;
this.lines = new ArrayList<>();
this.line = null;
this.match_table = match_table;
}
public void replaceFile(){
try{
File f1 = new File(filename);
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
while ((line=br.readLine())!= null){
String newline = "";
if (!line.equals("HALT")){
String[] buffer1 = line.split("\\s+");
newline += buffer1[0] + " ";
if (buffer1.length > 1){
String[] buffer2 = buffer1[1].split(",");
if (buffer2.length == 1){
if(buffer2[0].startsWith("R")){
buffer2[0] = match_table.get(buffer2[0]);
}
newline += buffer2[0];
} else {
for (int i = 0; i<buffer2.length; i++){
if (buffer2[i].startsWith("R")){
buffer2[i]= match_table.get(buffer2[i]);
}
if (i == 0){
newline += buffer2[i];
} else {
newline += ","+buffer2[i];
}
}
}
}
} else {
newline += line;
}
lines.add(newline);
/*for (String pattern : match_table.keySet()){
if (line.contains(pattern)){
line = line.replace(pattern, match_table.get(pattern));
}
}
lines.add(line);*/
}
fr.close();
br.close();
FileWriter fw = new FileWriter(f1);
BufferedWriter out = new BufferedWriter(fw);
for (String s : lines){
out.write(s);
out.newLine();
}
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}