/
ModifyAllFile.java
256 lines (243 loc) · 8.21 KB
/
ModifyAllFile.java
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* 文件名:ModifyAllFile.java
* 版权:Copyright 2007-2016 zxiaofan.com. Co. Ltd. All Rights Reserved.
* 描述: ModifyAllFile.java
* 修改人:zxiaofan
* 修改时间:2016年9月30日
* 修改内容:新增
*/
package com.zxiaofan.util.privateUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
/**
* 修改所有文件(加注释),便于提测时提全量。
*
* github.zxiaofan.com
*
* @author zxiaofan
*/
public class ModifyAllFile {
/**
* 支持多条规则(以;分隔,每条规则以|分隔后缀名和增加内容)(a|b|c表示以a或b后后缀的文件将在文件头增加c).
*/
static String rule = ".java||////AutoModify -By zxiaofan////;.jsp|.xml|<!-- AutoModify -By zxiaofan-->;";
/**
* 在末尾追加注释.
*/
static List<String> appendInEnd = Arrays.asList(".xml");
private static String encode = "utf-8";
public static void main(String[] args) {
System.out.println("请输入待修改的文件(夹)的绝对路径:");
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
String path = scanner.nextLine();
modify(path, rule);
}
/**
* 文件首行增加内容.
*
* @param path
* 待修改文件路径
* @param rules
* 修改规则
*/
public static void modify(String path, String rules) {
if (isNullOrEmpty(rules)) {
System.out.println("规则为空...");
return;
}
Map<String, String> mapRule = buildRuleMap(rules);
List<String> files = null;
if (null != mapRule && !mapRule.isEmpty()) {
files = getAllFilePath(path, mapRule.keySet());
}
try {
modifyFile(files, mapRule);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("修改完成");
}
/**
* 构建规则Map.
*
* @param rules
* 规则字符串
* @return Map
*/
private static Map<String, String> buildRuleMap(String rules) {
Map<String, String> mapRule = new HashMap<>();
String[] arr = rules.split(";");
for (String keyVal : arr) {
if (!isNullOrEmpty(keyVal)) {
String[] spl = keyVal.split("\\|");
for (int i = 0; i < spl.length - 1; i++) {
mapRule.put(spl[i], spl[spl.length - 1]);
}
}
}
mapRule.remove("");
return mapRule;
}
/**
* 按规则修改特定文件.
*
* @throws Exception
*
*/
private static void modifyFile(List<String> filePaths, Map<String, String> mapRule) throws Exception {
if (isNullOrEmpty(mapRule) || isNullOrEmpty(filePaths)) {
return;
}
String addNote = "";
String fileSuffix = "";
for (String path : filePaths) {
for (String suffix : mapRule.keySet()) {
if (path.endsWith(suffix)) {
fileSuffix = suffix;
addNote = mapRule.get(suffix);
break;
}
}
FileInputStream inputStream = new FileInputStream(path);
LineNumberReader reader = new LineNumberReader(new InputStreamReader(inputStream, encode));
StringBuffer content = new StringBuffer();
if (!appendInEnd.contains(fileSuffix)) {
content.append(addNote);
content.append("\r\n");
}
String s = reader.readLine();
while (s != null && !"null".equals(s)) {
content.append(s);
content.append("\r\n");
s = reader.readLine();
}
if (appendInEnd.contains(fileSuffix)) {
content.append(addNote);
}
content.append("\r\n");
reader.close();
// try {
// FileWriter fw = new FileWriter(path);
// fw.write(content.toString());
// fw.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
writeStringToFile(path, content.toString(), encode);
}
}
/**
* 将字符串数据存于本地(FileUtil工具类).
*
* @param fileName
* 文件名
* @param data
* 数据
* @param encode
* 编码(可为null)
* @throws IOException
* IO异常
*/
private static void writeStringToFile(String fileName, String data, String encode) throws IOException {
FileOutputStream output = null;
File file = new File(fileName);
if (file.exists()) {
if (file.isDirectory())
throw new IOException("File '" + file + "' exists but is a directory");
if (!file.canWrite())
throw new IOException("File '" + file + "' cannot be written to");
} else {
File parent = file.getParentFile();
if (parent != null && !parent.exists() && !parent.mkdirs())
throw new IOException("File '" + file + "' could not be created");
}
try {
output = new FileOutputStream(file);
if (data != null)
if (encode == null)
output.write(data.getBytes());
else
output.write(data.getBytes(encode));
} finally {
if (null != output) {
try {
output.close();
} catch (IOException e) {
System.out.print("");
}
}
}
}
/**
* 所有待转换文件绝对路径.
*
* @param path
* @return 所有带转换文件路径
*/
private static List<String> getAllFilePath(String path, Set<String> suffixSet) {
/**
* 所有待转换文件绝对路径.
*/
List<String> filePaths = new ArrayList<>();
File root = new File(path);
File[] files = root.listFiles();
if (files == null) {
filePaths.add(path);
return filePaths;
}
for (File file : files) {
if (file.isDirectory()) {
// 递归调用
List<String> list = getAllFilePath(file.getAbsolutePath(), suffixSet);
if (!isNullOrEmpty(list)) {
filePaths.addAll(list);
}
// System.out.println(filePath + "目录下所有子目录及其文件" + file.getAbsolutePath());
} else {
// System.out.println(filePath + "目录下所有文件" + file.getAbsolutePath());
for (String suf : suffixSet) {
if (file.getAbsolutePath().endsWith(suf)) {
filePaths.add(file.getAbsolutePath());
}
}
}
}
return filePaths;
}
/**
* 是否为null或空字符串或空集合.
*
* @param param
* 参数
* @return bool
*/
@SuppressWarnings("rawtypes")
private static boolean isNullOrEmpty(Object param) {
if (param instanceof String) {
if (null == param || "".equals(((String) param).trim())) {
return true;
}
} else if (param instanceof List) {
if (null == param || ((List) param).isEmpty()) {
return true;
}
} else if (param instanceof Map) {
if (null == param || ((Map) param).isEmpty()) {
return true;
}
}
return false;
}
}