-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXFileUtil.java
More file actions
142 lines (137 loc) · 3.78 KB
/
Copy pathXFileUtil.java
File metadata and controls
142 lines (137 loc) · 3.78 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
package main;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
public class XFileUtil {
/*复制单个文件
* @param a 将复制文件
* @param b 生产文件
*/
public static void copyAtoB(File a,File b) throws IOException{
if(a == null || !a.exists()){
return;
}
InputStream in = null;
FileOutputStream out = null;
in = new FileInputStream(a);
out = new FileOutputStream(b);
byte[] buffer = new byte[1024];
int byteRead = 0;
while((byteRead = in.read(buffer)) != -1){
out.write(buffer, 0, byteRead);
}
in.close();
out.close();
}
/*
* @param aDir 要复制的目录
* @param bDir 生成目录
*/
public static void copyADirToBDir(File aDir,File bDir) throws IOException{
if(!aDir.exists() || aDir == null){
return;
}
if(!bDir.exists()){
bDir.mkdirs();
}
String[] file = aDir.list();
File temp = null;
for(int i = 0; i < file.length ;i++){
if(!aDir.isDirectory()){//无目录结构
temp = new File(aDir.getAbsolutePath()+file[i]);
}else{//有目录结构
temp = new File(aDir.getAbsolutePath()+File.separator+file[i]);
}
if(!temp.isDirectory()){//无目录
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(bDir.getAbsolutePath() + File.separator +
(temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ( (len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}else{//有子文件
copyADirToBDir(new File(aDir.getAbsolutePath()+File.separator+file[i]),new File(bDir.getAbsolutePath()+File.separator+file[i]));
}
}
}
/*
* @return 判断两个文件是否相同 true:相同,false:不同(只判断文件内容,对文件名不做判断)
*/
public static boolean isAFileEqualsBFile(File a,File b){
if(a == null || b == null){
return false;
}
if(a.length() == b.length()){
if(getFileMD5(a).equals(getFileMD5(b))){
return true;
}
return false;
}
return false;
}
// 计算文件的 MD5 值
public static String getFileMD5(File file) {
if (!file.isFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[8192];
int len;
try {
digest =MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer)) != -1) {
digest.update(buffer, 0, len);
}
BigInteger bigInt = new BigInteger(1, digest.digest());
return bigInt.toString(16);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 计算文件的 SHA-1 值
public static String getFileSha1(File file) {
if (!file.isFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[8192];
int len;
try {
digest =MessageDigest.getInstance("SHA-1");
in = new FileInputStream(file);
while ((len = in.read(buffer)) != -1) {
digest.update(buffer, 0, len);
}
BigInteger bigInt = new BigInteger(1, digest.digest());
return bigInt.toString(16);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}