forked from 0opslab/opslabJutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileUtilTest.java
284 lines (239 loc) · 11.8 KB
/
FileUtilTest.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package com.opslab;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class FileUtilTest extends SupportTest {
@Test
public void testCountLines() throws IOException {
String file = System.getProperty("user.dir") + "/Junit/Resource/English.txt";
assertEquals("文件行数计算有误", 5, FileUtil.countLines(new File(file)));
}
@Test
public void testHash() {
String file = System.getProperty("user.dir") + "/Junit/Resource/ali.gif";
assertEquals("文件Hash校验错误", "4FE6FF69F7257F2E8C36B0752B5393BF", FileUtil.hash(new File(file)).toUpperCase());
}
@Test
public void testFileType() throws IOException {
String efile = System.getProperty("user.dir") + "/Junit/Resource/ali.gif";
String hfile = System.getProperty("user.dir") + "/Junit/Resource/tgepng";
assertEquals("文件类型判断有错", "gif", FileUtil.fileType(new File(efile)));
assertEquals("文件类型判断有错", "png", FileUtil.fileType(new File(hfile)));
assertEquals("获取文件的mime类型错误", "image/gif", FileUtil.mimeType(efile));
}
@Test
public void testModify() {
String exefile = System.getProperty("user.dir") + "/Junit/Resource/cmdexe";
Date date = FileUtil.modifyTime(new File(exefile));
//assertEquals("获取文件修改的最后时间错误","2010-11-21 11:24:03",DateUtil.DateTime(date));
}
@Test
public void testLines() {
System.out.println("=====按List讲文件全部读入到List中======");
PrintUtil.print("全英文文件测试");
String efile = System.getProperty("user.dir") + "/Junit/Resource/English.txt";
List<String> lines = FileUtil.lines(new File(efile));
PrintUtil.print(lines);
PrintUtil.print("读取文件的前3行");
lines = FileUtil.lines(new File(efile), 3);
PrintUtil.print(lines);
PrintUtil.print("GBK文件测试");
String gbkfile = System.getProperty("user.dir") + "/Junit/Resource/GBK.txt";
List<String> gbklines = FileUtil.lines(new File(gbkfile));
PrintUtil.print(gbklines);
gbklines = FileUtil.lines(new File(gbkfile), "GBK");
PrintUtil.print(gbklines);
PrintUtil.print("读取文件的前3行");
lines = FileUtil.lines(new File(gbkfile), 3, "GBK");
PrintUtil.print(lines);
PrintUtil.print("UTF8文件测试");
String utf8file = System.getProperty("user.dir") + "/Junit/Resource/UTF8.txt";
List<String> utf8lines = FileUtil.lines(new File(utf8file));
PrintUtil.print(utf8lines);
utf8lines = FileUtil.lines(new File(utf8file), "UTF-8");
PrintUtil.print(utf8lines);
PrintUtil.print("读取文件的前3行");
lines = FileUtil.lines(new File(utf8file), 3, "UTF-8");
PrintUtil.print(lines);
}
@Test
public void testCleanFile() {
System.out.println("快速清空一个超大文件");
String clearFile = System.getProperty("user.dir") + "/Junit/Resource/temp/GBKTOUTF8.txt";
FileUtil.cleanFile(new File(clearFile));
}
@Test
public void testCopy() throws Exception {
System.out.println("测试文件拷贝");
String copy = System.getProperty("user.dir") + "/Junit/Resource/GBKTOUTF8.txt";
String dest = System.getProperty("user.dir") + "/Junit/Resource/temp/GBKTOUTF8.txt";
FileUtil.copy(copy, dest);
}
@Test
public void testAppendLine() {
System.out.println("向文件中追加行");
String line1 = "E-String";
String line2 = "中文字符串";
File efile = new File(System.getProperty("user.dir") + "/Junit/Resource/EnglishWrite.txt");
File gbkfile = new File(System.getProperty("user.dir") + "/Junit/Resource/GBKWrite.txt");
File utf8file = new File(System.getProperty("user.dir") + "/Junit/Resource/UTF8Write.txt");
FileUtil.appendLine(efile, line1);
FileUtil.appendLine(efile, line2);
FileUtil.appendLine(efile, line1, "UTF-8");
FileUtil.appendLine(efile, line2, "UTF-8");
FileUtil.appendLine(gbkfile, line1);
FileUtil.appendLine(gbkfile, line2);
FileUtil.appendLine(gbkfile, line1, "GBK");
FileUtil.appendLine(gbkfile, line2, "GBK");
FileUtil.appendLine(utf8file, line1);
FileUtil.appendLine(utf8file, line2);
FileUtil.appendLine(utf8file, line1, "UTF-8");
FileUtil.appendLine(utf8file, line2, "UTF-8");
}
@Test
public void testDeleteFile() {
System.out.println("删除文件");
String delete = System.getProperty("user.dir") + "/Junit/Resource/temp/delete.txt";
FileUtil.deleteFile(new File(delete));
}
@Test
public void testDeleteBigFile() {
System.out.println("快速删除超大文件");
String delete = System.getProperty("user.dir") + "/Junit/Resource/temp/bigfile.txt";
FileUtil.deleteBigFile(new File(delete));
}
@Test
public void testCopyDir() {
FileUtil.copyDir(new File(System.getProperty("user.dir") + "/conf/"), System.getProperty("user.dir") + "/Junit/Resource/temp/copy");
}
@Test
public void testCreateFiles() {
System.out.println("创建文件,支持多级目录");
String path = System.getProperty("user.dir") + "/Junit/Resource/temp/";
String file1 = path + "/test/test1.txt";
String file2 = path + "/test/test1/test.txt";
String file3 = path + "/test/test/";
FileUtil.createFiles(file1);
FileUtil.createFiles(file2);
FileUtil.createFiles(file3);
}
@Test
public void testDeleteDir() {
String path = System.getProperty("user.dir") + "/Junit/Resource/temp/";
FileUtil.deleteDir(new File(path + "/test/"));
}
@Test
public void testCreatePath() {
System.out.println("创建文件夹,支持多级目录");
String path = System.getProperty("user.dir") + "/Junit/Resource/temp/";
String path1 = path + "/test/test2/";
String path2 = path + "/test/test3";
FileUtil.createPaths(path1);
FileUtil.createPaths(path2);
}
@Test
public void testSimpleEncoding() {
System.out.println("简单的利用文件头进行文件的编码探测!");
String path = System.getProperty("user.dir") + "/Junit/Resource/";
System.out.println(FileUtil.simpleEncoding(path + "GBK.txt"));
System.out.println("利用cpdetector进行文件编码探测");
System.out.println(FileUtil.cpdetector(new File(path + "GBK.txt")));
}
@Test
public void testListFile() {
System.out.println("罗列指定目录下的所有文件");
String path = System.getProperty("user.dir") + "/Junit/Resource/temp/";
List<File> files = FileUtil.listFile(new File(path));
PrintUtil.print(files);
List<File> files1 = FileUtil.listFile(path);
PrintUtil.print(files1);
}
@Test
public void testCopyDirs() {
System.out.println("复制文件夹");
String path = System.getProperty("user.dir") + "/Junit/Resource/temp";
FileUtil.copyDir(path + "/test/", path + "/testcopy/");
}
@Test
public void testListFileFilter() {
System.out.println("罗列指定目录下的特定后缀的文件");
String path = System.getProperty("user.dir") + "/Junit/Resource/";
List<File> files = FileUtil.listFileFilter(new File(path), ".txt");
PrintUtil.print(files);
}
@Test
public void testSearchFile() {
System.out.println("在指定的目录下搜索指定的文件");
String path = System.getProperty("user.dir") + "/Junit/Resource/";
List<File> files = FileUtil.searchFile(new File(path), "GBK.txt");
PrintUtil.print(files);
}
@Test
public void testSearchReg() {
System.out.println("在指定的目录下搜索符合某正则的文件");
//匹配字母和数字组成的exe文件
String reg = "\\w{1,}\\.png$";
String path = System.getProperty("user.dir") + "/Junit/Resource/";
List<File> files = FileUtil.searchFileReg(new File(path), reg);
PrintUtil.print(files);
}
@Test
public void testWriteFile(){
String path = System.getProperty("user.dir")+"/Junit/Resource/temp/";
String etemp ="Two roads diverged in a yellow wood,\n" +
"And sorry I could not travel both\n" +
"And be one traveler, long I stood\n" +
"And looked down one as far as I could\n" +
"To where it bent in the undergrowth;\n" +
"Then took the other, as just as fair,\n" +
"And having perhaps the better claim,\n" +
"Because it was grassy and wanted wear;\n" +
"Though as for that the passing there\n" +
"Had worn them really about the same,\n" +
"And both that morning equally layn" +
"In leaves no step had trodden black.\n" +
"Oh, I kept the first for another day!\n" +
"Yet knowing how way leads on to way,\n" +
"I doubted if I should ever come back.\n" +
"I shall be telling this with a sigh\n" +
"Somewhere ages and ages hence:\n" +
"Two roads diverged in a wood,and\n" +
"I took the one less traveled by,\n" +
"And that has made al lthe difference.";
String ctemp ="黄色的树林里分出两条路\n" +
"可惜我不能同时去涉足\n" +
"我在那路口久久伫立\n" +
"我向着一条路极目望去\n" +
"直到它消失在丛林深处\n" +
"但我却选择了另外一条路\n" +
"它荒草萋萋,十分幽寂 \n" +
"显得更诱人,更美丽 \n" +
"虽然在这两条小路上\n" +
"都很少留下旅人的足迹\n" +
"虽然那天清晨落叶满地\n" +
"两条路都未经脚印污染\n" +
"呵,留下一条路等改日再见\n" +
"但我知道路径延绵无尽头\n" +
"恐怕我难以再回返\n" +
"也许多少年后在某一个地方\n" +
"我将轻声叹息把往事回顾\n" +
" 一片森林里分出两条路\n" +
"而我却选择了人迹更少的一条\n" +
"从此决定了我一生的道路";
FileUtil.write(new File(path+"efile1.TXT"),etemp);
FileUtil.write(new File(path+"efile2.TXT"),etemp);
FileUtil.writeAppend(new File(path + "efile2.TXT"), etemp);
FileUtil.write(new File(path + "cfile1.TXT"), ctemp);
FileUtil.write(new File(path+"cfile2.TXT"),ctemp);
FileUtil.writeAppend(new File(path + "cfile2.TXT"), ctemp);
FileUtil.write(new File(path + "efile1_UTF8.TXT"), etemp, CharsetUtil.UTF_8);
FileUtil.write(new File(path+"efile2_UTF8.TXT"),etemp,CharsetUtil.UTF_8);
FileUtil.writeAppend(new File(path + "efile2_UTF8.TXT"), etemp,CharsetUtil.UTF_8);
FileUtil.write(new File(path + "cfile1_UTF8.TXT"), ctemp,CharsetUtil.UTF_8);
FileUtil.write(new File(path+"cfile2_UTF8.TXT"),ctemp,CharsetUtil.UTF_8);
FileUtil.writeAppend(new File(path + "cfile2_UTF8.TXT"), ctemp,CharsetUtil.UTF_8);
}
}