-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReplacer.java
More file actions
97 lines (81 loc) · 3.15 KB
/
FileReplacer.java
File metadata and controls
97 lines (81 loc) · 3.15 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.replace.sample;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class FileReplacer {
private static final BiPredicate<Path, BasicFileAttributes> predicateRegularFile = (p, bfa) -> bfa.isRegularFile()
&& p.toFile().exists();
private static final BiPredicate<Path, BasicFileAttributes> findByFileNamePattern(final Pattern pattern) {
return (p, bfa) -> pattern.matcher(p.toFile().toURI().toString()).find();
}
private static final String replaceWord(final String v, final Map<String, String> wordsToReplace) {
Set<String> keys = wordsToReplace.keySet();
String a = v;
for (String key : keys) {
a = a.replace(key, wordsToReplace.get(key));
}
return a;
}
final Map<String, String> wordsToReplace = wordsToReplace();
Map<String, String> wordsToReplace() {
final Map<String, String> wordsToReplace = new HashMap<String, String>();
// wordsToReplace.put("are", "r");
// wordsToReplace.put("you", "u");
// wordsToReplace.put("to", "2");
wordsToReplace.put("\":false", "\":0");
wordsToReplace.put("\":true", "\":1");
wordsToReplace.put("\"aid_GstxIWObRA_p_salescode\":\"HUI\"", "\"aid_gstxiwobra_p_salescode\":\"HuI\"");
//
return wordsToReplace;
}
private Consumer<File> fileConsumer = new Consumer<File>() {
@Override
public void accept(final File file) {
System.out.println(file.getAbsolutePath());
try {
Path path = Paths.get(file.toURI());
Stream<String> lines = Files.lines(path);
List<String> replaced = lines.map(v -> replaceWord(v, wordsToReplace)).collect(Collectors.toList());
Files.write(path, replaced);
lines.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
};
private List<File> files(final File file) {
final Path start = Paths.get(file.toURI());
final Pattern pattern = Pattern.compile("[\\*.prd]$");
try {
return Files.find(start, Integer.MAX_VALUE, predicateRegularFile.and(findByFileNamePattern(pattern))).map(v -> v.toFile())
.sorted() /* */
.collect(Collectors.toList());
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void doExecute(final File path) {
List<File> files = files(path);
files.stream().forEach(fileConsumer);
}
public static void main(String[] args) {
final FileReplacer replacer = new FileReplacer();
final File path = new File("/log/torepl");
replacer.doExecute(path);
}
}