forked from AlEinstein/javaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtil.java
More file actions
584 lines (536 loc) · 17.4 KB
/
Copy pathFileUtil.java
File metadata and controls
584 lines (536 loc) · 17.4 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/**
* @author striner
* @create 2018/12/1 21:40
*/
package common.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Properties;
import javax.servlet.http.HttpServletResponse;
/**
* 封装一些基本的文件操作
*
* Created by mulin on 2018/12/02..
*/
public class FileUtil {
public static void writeFile(String fileName, String content) {
writeFile(fileName, content, "utf-8");
}
public static void writeFile(String fileName, String content, String charset) {
try {
createFolder(fileName, true); //若路径不存在会报错(FileNotFoundException),故需先创建文件路径
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), charset));
out.write(content);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void writeFile(String fileName, InputStream is) throws IOException {
FileOutputStream fos = new FileOutputStream(fileName);
byte[] bs = new byte[512];
int n = 0;
while ((n = is.read(bs)) != -1) {
fos.write(bs, 0, n);
}
is.close();
fos.close();
}
public static String readFile(String fileName) {
try {
File file = new File(fileName);
String charset = getCharset(file);
StringBuffer sb = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), charset));
String str;
while ((str = in.readLine()) != null) {
sb.append(str + "\r\n");
}
in.close();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public static boolean isExistFileDir(String dir) {
boolean isExist = false;
File fileDir = new File(dir);
if (fileDir.isDirectory()) {
File[] files = fileDir.listFiles();
if ((files != null) && (files.length != 0)) {
isExist = true;
}
}
return isExist;
}
public static String getCharset(File file) {
String charset = "GBK";
byte[] first3Bytes = new byte[3];
try {
boolean checked = false;
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
bis.mark(0);
int read = bis.read(first3Bytes, 0, 3);
if (read == -1)
return charset;
if ((first3Bytes[0] == -1) && (first3Bytes[1] == -2)) {
charset = "UTF-16LE";
checked = true;
} else if ((first3Bytes[0] == -2) && (first3Bytes[1] == -1)) {
charset = "UTF-16BE";
checked = true;
} else if ((first3Bytes[0] == -17) && (first3Bytes[1] == -69) && (first3Bytes[2] == -65)) {
charset = "UTF-8";
checked = true;
}
bis.reset();
if (!checked) {
int loc = 0;
while ((read = bis.read()) != -1) {
loc++;
if (read < 240) {
if ((128 > read) || (read > 191)) {
if ((192 <= read) && (read <= 223)) {
read = bis.read();
if ((128 > read) || (read > 191)) {
break;
}
} else if ((224 <= read) && (read <= 239)) {
read = bis.read();
if ((128 <= read) && (read <= 191)) {
read = bis.read();
if ((128 <= read) && (read <= 191)) {
charset = "UTF-8";
}
}
}
}
}
}
}
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
return charset;
}
public static byte[] readByte(InputStream is) {
try {
byte[] r = new byte[is.available()];
is.read(r);
return r;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] readByte(String fileName) {
try {
FileInputStream fis = new FileInputStream(fileName);
byte[] r = new byte[fis.available()];
fis.read(r);
fis.close();
return r;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static boolean writeByte(String fileName, byte[] b) {
try {
BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(fileName));
fos.write(b);
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static boolean deleteFile(String path) {
return deleteFile(new File(path));
}
public static boolean deleteFile(File file) {
return file.delete();
}
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
/**
* 将对象序列化到文档中
*
* @param obj
* @param fileName
*/
public static void serializeToFile(Object obj, String fileName) {
try {
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(fileName));
out.writeObject(obj);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 将文档中的数据反序列化成对象
*
* @param fileName
* @return
*/
public static Object deserializeFromFile(String fileName) {
try {
File file = new File(fileName);
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
Object obj = in.readObject();
in.close();
return obj;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String inputStream2String(InputStream input, String charset) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(input, charset));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line + "\n");
}
return buffer.toString();
}
public static String inputStream2String(InputStream input) throws IOException {
return inputStream2String(input, "utf-8");
}
public static File[] getFiles(String path) {
File file = new File(path);
return file.listFiles();
}
public static void createFolderFile(String path) {
createFolder(path, true);
}
public static void createFolder(String path, boolean isFile) {
if (isFile) {
path = path.substring(0, path.lastIndexOf(File.separator));
}
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
}
public static void createFolder(String pathName) {
File dir = new File(pathName);
dir.mkdir();
}
public static void renameFolder(String path, String newName) {
File file = new File(path);
if (file.exists()) {
file.renameTo(new File(newName));
}
}
/**
* 仅获取该路径下的所有文件夹
*
* @param dir
* @return
*/
public static ArrayList<File> getDiretoryOnly(File dir) {
ArrayList dirs = new ArrayList();
if ((dir != null) && (dir.exists()) && (dir.isDirectory())) {
File[] files = dir.listFiles(new FileFilter() {
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
return false;
}
});
for (int i = 0; i < files.length; i++) {
dirs.add(files[i]);
}
}
return dirs;
}
/**
* 仅获取该路径下的所有文件
*
* @param dir
* @return
*/
public static ArrayList<File> getFileOnly(File dir) {
ArrayList dirs = new ArrayList();
File[] files = dir.listFiles(new FileFilter() {
public boolean accept(File file) {
if (file.isFile()) {
return true;
}
return false;
}
});
for (int i = 0; i < files.length; i++) {
dirs.add(files[i]);
}
return dirs;
}
public static boolean copyFile(String from, String to) {
File fromFile = new File(from);
File toFile = new File(to);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(fromFile);
fos = new FileOutputStream(toFile);
byte[] buf = new byte[4096];
int bytesRead;
while ((bytesRead = fis.read(buf)) != -1) {
fos.write(buf, 0, bytesRead);
}
fos.flush();
fos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 文件备份
*
* @param filePath
*/
public static void backupFile(String filePath) {
String backupName = filePath + ".bak";
File file = new File(backupName);
if (file.exists()) {
file.delete();
}
copyFile(filePath, backupName);
}
/**
* 获得文件后缀名
*
* @param file
* @return
*/
public static String getFileExt(File file) {
if (file.isFile()) {
return getFileExt(file.getName());
}
return "";
}
public static String getFileExt(String fileName) {
int pos = fileName.lastIndexOf(".");
if (pos > -1) {
return fileName.substring(pos + 1).toLowerCase();
}
return "";
}
public static void copyDir(String fromDir, String toDir) {
new File(toDir).mkdirs();
File[] file = new File(fromDir).listFiles();
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
String fromName = file[i].getAbsolutePath();
String toFile = toDir + "/" + file[i].getName();
copyFile(fromName, toFile);
}
if (file[i].isDirectory())
copyDir(fromDir + "/" + file[i].getName(), toDir + "/" + file[i].getName());
}
}
public static String getFileSize(File file) throws IOException {
if (file.isFile()) {
FileInputStream fis = new FileInputStream(file);
int size = fis.available();
fis.close();
return getSize(size);
}
return "";
}
public static String getSize(double size) {
DecimalFormat df = new DecimalFormat("0.00");
if (size > 1048576.0D) {
double ss = size / 1048576.0D;
return df.format(ss) + " M";
}
if (size > 1024.0D) {
double ss = size / 1024.0D;
return df.format(ss) + " KB";
}
return size + " bytes";
}
public static void downLoadFile(HttpServletResponse response, String fullPath, String fileName) throws IOException {
OutputStream outp = response.getOutputStream();
File file = new File(fullPath);
if (file.exists()) {
response.setContentType("APPLICATION/OCTET-STREAM");
String fileDisplay = URLEncoder.encode(fileName, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + fileDisplay);
FileInputStream in = null;
try {
outp = response.getOutputStream();
in = new FileInputStream(fullPath);
byte[] b = new byte[1024];
int i = 0;
while ((i = in.read(b)) > 0) {
outp.write(b, 0, i);
}
outp.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
in = null;
}
if (outp != null) {
outp.close();
outp = null;
response.flushBuffer();
}
}
} else {
outp.write("文件不存在!".getBytes("utf-8"));
}
}
public static String getParentDir(String baseDir, String currentFile) {
File f = new File(currentFile);
String parentPath = f.getParent();
String path = parentPath.replace(baseDir, "");
return path.replace(File.separator, "/");
}
public static String readFromProperties(String fileName, String key) {
String value = "";
InputStream stream = null;
try {
stream = new BufferedInputStream(new FileInputStream(fileName));
Properties prop = new Properties();
prop.load(stream);
value = prop.getProperty(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return value;
}
public static boolean saveProperties(String fileName, String key, String value) {
StringBuffer sb = new StringBuffer();
boolean isFound = false;
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "utf-8"));
String str;
while ((str = in.readLine()) != null) {
if (str.startsWith(key)) {
sb.append(key + "=" + value + "\r\n");
isFound = true;
} else {
sb.append(str + "\r\n");
}
}
if (!isFound) {
sb.append(key + "=" + value + "\r\n");
}
writeFile(fileName, sb.toString(), "utf-8");
return true;
} catch (Exception ex) {
boolean bool1;
ex.printStackTrace();
return false;
} finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String getMd5ByFile(File file) throws FileNotFoundException {
String value = null;
FileInputStream in = new FileInputStream(file);
try {
MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(byteBuffer);
BigInteger bi = new BigInteger(1, md5.digest());
value = bi.toString(16);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return value;
}
/**
* 将文件保存到硬盘指定目录下
*
* @param in
* @param filePath
* @throws IOException
*/
public static void saveToDisk(InputStream in, String filePath) throws IOException {
File file = new File(filePath);
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.flush();
fos.close();
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}