forked from chenssy89/jutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtils.java
More file actions
307 lines (288 loc) · 7.63 KB
/
FileUtils.java
File metadata and controls
307 lines (288 loc) · 7.63 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
package com.JUtils.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import com.JUtils.date.DateUtils;
import com.JUtils.math.RandomUtils;
/**
* @desc:文件工具类
*
* @Author:chenssy
* @date:2014年8月7日
*/
public class FileUtils {
private static final String FOLDER_SEPARATOR = "/";
private static final char EXTENSION_SEPARATOR = '.';
/**
* @desc:判断指定路径是否存在,如果不存在,根据参数决定是否新建
* @autor:chenssy
* @date:2014年8月7日
*
* @param filePath
* 指定的文件路径
* @param isNew
* true:新建、false:不新建
* @return 存在返回TRUE,不存在返回FALSE
*/
public static boolean isExist(String filePath,boolean isNew){
File file = new File(filePath);
if(!file.exists() && isNew){
return file.mkdirs(); //新建文件路径
}
return false;
}
/**
* 获取文件名,构建结构为 prefix + yyyyMMddHH24mmss + 10位随机数 + suffix + .type
* @autor:chenssy
* @date:2014年8月11日
*
* @param type
* 文件类型
* @param prefix
* 前缀
* @param suffix
* 后缀
* @return
*/
public static String getFileName(String type,String prefix,String suffix){
String date = DateUtils.getCurrentTime("yyyyMMddHH24mmss"); //当前时间
String random = RandomUtils.generateNumberString(10); //10位随机数
//返回文件名
return prefix + date + random + suffix + "." + type;
}
/**
* 获取文件名,文件名构成:当前时间 + 10位随机数 + .type
* @autor:chenssy
* @date:2014年8月11日
*
* @param type
* 文件类型
* @return
*/
public static String getFileName(String type){
return getFileName(type, "", "");
}
/**
* 获取文件名,文件构成:当前时间 + 10位随机数
* @autor:chenssy
* @date:2014年8月11日
*
* @return
*/
public static String getFileName(){
String date = DateUtils.getCurrentTime("yyyyMMddHH24mmss"); //当前时间
String random = RandomUtils.generateNumberString(10); //10位随机数
//返回文件名
return date + random;
}
/**
* 获取指定文件的大小
*
* @param file
* @return
* @throws Exception
*
* @author:chenssy
* @date : 2016年4月30日 下午9:10:12
*/
@SuppressWarnings("resource")
public static long getFileSize(File file) throws Exception {
long size = 0;
if (file.exists()) {
FileInputStream fis = null;
fis = new FileInputStream(file);
size = fis.available();
} else {
file.createNewFile();
}
return size;
}
/**
* 删除所有文件,包括文件夹
*
* @author : chenssy
* @date : 2016年5月23日 下午12:41:08
*
* @param dirpath
*/
public void deleteAll(String dirpath) {
File path = new File(dirpath);
try {
if (!path.exists())
return;// 目录不存在退出
if (path.isFile()) // 如果是文件删除
{
path.delete();
return;
}
File[] files = path.listFiles();// 如果目录中有文件递归删除文件
for (int i = 0; i < files.length; i++) {
deleteAll(files[i].getAbsolutePath());
}
path.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 复制文件或者文件夹
*
* @author : chenssy
* @date : 2016年5月23日 下午12:41:59
*
* @param inputFile
* 源文件
* @param outputFile
* 目的文件
* @param isOverWrite
* 是否覆盖文件
* @throws java.io.IOException
*/
public static void copy(File inputFile, File outputFile, boolean isOverWrite)
throws IOException {
if (!inputFile.exists()) {
throw new RuntimeException(inputFile.getPath() + "源目录不存在!");
}
copyPri(inputFile, outputFile, isOverWrite);
}
/**
* 复制文件或者文件夹
*
* @author : chenssy
* @date : 2016年5月23日 下午12:43:24
*
* @param inputFile
* 源文件
* @param outputFile
* 目的文件
* @param isOverWrite
* 是否覆盖文件
* @throws java.io.IOException
*/
private static void copyPri(File inputFile, File outputFile, boolean isOverWrite) throws IOException {
if (inputFile.isFile()) { //文件
copySimpleFile(inputFile, outputFile, isOverWrite);
} else {
if (!outputFile.exists()) { //文件夹
outputFile.mkdirs();
}
// 循环子文件夹
for (File child : inputFile.listFiles()) {
copy(child, new File(outputFile.getPath() + "/" + child.getName()), isOverWrite);
}
}
}
/**
* 复制单个文件
*
* @author : chenssy
* @date : 2016年5月23日 下午12:44:07
*
* @param inputFile
* 源文件
* @param outputFile
* 目的文件
* @param isOverWrite
* 是否覆盖
* @throws java.io.IOException
*/
private static void copySimpleFile(File inputFile, File outputFile,
boolean isOverWrite) throws IOException {
if (outputFile.exists()) {
if (isOverWrite) { //可以覆盖
if (!outputFile.delete()) {
throw new RuntimeException(outputFile.getPath() + "无法覆盖!");
}
} else {
// 不允许覆盖
return;
}
}
InputStream in = new FileInputStream(inputFile);
OutputStream out = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int read = 0;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
out.close();
}
/**
* 获取文件的MD5
*
* @author : chenssy
* @date : 2016年5月23日 下午12:50:38
*
* @param file
* 文件
* @return
*/
public static String getFileMD5(File file){
if (!file.exists() || !file.isFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[1024];
int len;
try {
digest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer, 0, 1024)) != -1) {
digest.update(buffer, 0, len);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
BigInteger bigInt = new BigInteger(1, digest.digest());
return bigInt.toString(16);
}
/**
* 获取文件的后缀
*
* @author : chenssy
* @date : 2016年5月23日 下午12:51:59
*
* @param file
* 文件
* @return
*/
public static String getFileSuffix(String file) {
if (file == null) {
return null;
}
int extIndex = file.lastIndexOf(EXTENSION_SEPARATOR);
if (extIndex == -1) {
return null;
}
int folderIndex = file.lastIndexOf(FOLDER_SEPARATOR);
if (folderIndex > extIndex) {
return null;
}
return file.substring(extIndex + 1);
}
/**
* 文件重命名
*
* @author : chenssy
* @date : 2016年5月23日 下午12:56:05
*
* @param oldPath
* 老文件
* @param newPath
* 新文件
*/
public boolean renameDir(String oldPath, String newPath) {
File oldFile = new File(oldPath);// 文件或目录
File newFile = new File(newPath);// 文件或目录
return oldFile.renameTo(newFile);// 重命名
}
}