Skip to content

Commit 37211ad

Browse files
committed
see 09/09 log
1 parent 09ee984 commit 37211ad

File tree

4 files changed

+382
-238
lines changed

4 files changed

+382
-238
lines changed

utilcode/src/main/java/com/blankj/utilcode/util/FileUtils.java

Lines changed: 87 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public static boolean isDir(final String dirPath) {
111111
* @return {@code true}: 是<br>{@code false}: 否
112112
*/
113113
public static boolean isDir(final File file) {
114-
return isFileExists(file) && file.isDirectory();
114+
return file != null && file.exists() && file.isDirectory();
115115
}
116116

117117
/**
@@ -131,7 +131,7 @@ public static boolean isFile(final String filePath) {
131131
* @return {@code true}: 是<br>{@code false}: 否
132132
*/
133133
public static boolean isFile(final File file) {
134-
return isFileExists(file) && file.isFile();
134+
return file != null && file.exists() && file.isFile();
135135
}
136136

137137
/**
@@ -184,6 +184,16 @@ public static boolean createOrExistsFile(final File file) {
184184
}
185185
}
186186

187+
/**
188+
* 判断文件是否存在,存在则在创建之前删除
189+
*
190+
* @param filePath 文件路径
191+
* @return {@code true}: 创建成功<br>{@code false}: 创建失败
192+
*/
193+
public static boolean createFileByDeleteOldFile(final String filePath) {
194+
return createFileByDeleteOldFile(getFileByPath(filePath));
195+
}
196+
187197
/**
188198
* 判断文件是否存在,存在则在创建之前删除
189199
*
@@ -209,22 +219,24 @@ public static boolean createFileByDeleteOldFile(final File file) {
209219
*
210220
* @param srcDirPath 源目录路径
211221
* @param destDirPath 目标目录路径
222+
* @param listener 是否覆盖监听器
212223
* @param isMove 是否移动
213224
* @return {@code true}: 复制或移动成功<br>{@code false}: 复制或移动失败
214225
*/
215-
private static boolean copyOrMoveDir(final String srcDirPath, final String destDirPath, final boolean isMove) {
216-
return copyOrMoveDir(getFileByPath(srcDirPath), getFileByPath(destDirPath), isMove);
226+
private static boolean copyOrMoveDir(final String srcDirPath, final String destDirPath, final OnReplaceListener listener, final boolean isMove) {
227+
return copyOrMoveDir(getFileByPath(srcDirPath), getFileByPath(destDirPath), listener, isMove);
217228
}
218229

219230
/**
220231
* 复制或移动目录
221232
*
222-
* @param srcDir 源目录
223-
* @param destDir 目标目录
224-
* @param isMove 是否移动
233+
* @param srcDir 源目录
234+
* @param destDir 目标目录
235+
* @param listener 是否覆盖监听器
236+
* @param isMove 是否移动
225237
* @return {@code true}: 复制或移动成功<br>{@code false}: 复制或移动失败
226238
*/
227-
private static boolean copyOrMoveDir(final File srcDir, final File destDir, final boolean isMove) {
239+
private static boolean copyOrMoveDir(final File srcDir, final File destDir, final OnReplaceListener listener, final boolean isMove) {
228240
if (srcDir == null || destDir == null) return false;
229241
// 如果目标目录在源目录中则返回false,看不懂的话好好想想递归怎么结束
230242
// srcPath : F:\\MyGithub\\AndroidUtilCode\\utilcode\\src\\test\\res
@@ -235,17 +247,26 @@ private static boolean copyOrMoveDir(final File srcDir, final File destDir, fina
235247
if (destPath.contains(srcPath)) return false;
236248
// 源文件不存在或者不是目录则返回false
237249
if (!srcDir.exists() || !srcDir.isDirectory()) return false;
250+
if (destDir.exists()) {
251+
if (listener.onReplace()) {// 需要覆盖则删除旧目录
252+
if (!deleteAllInDir(destDir)) {// 删除文件失败的话返回false
253+
return false;
254+
}
255+
} else {// 不需要覆盖直接返回即可true
256+
return true;
257+
}
258+
}
238259
// 目标目录不存在返回false
239260
if (!createOrExistsDir(destDir)) return false;
240261
File[] files = srcDir.listFiles();
241262
for (File file : files) {
242263
File oneDestFile = new File(destPath + file.getName());
243264
if (file.isFile()) {
244265
// 如果操作失败返回false
245-
if (!copyOrMoveFile(file, oneDestFile, isMove)) return false;
266+
if (!copyOrMoveFile(file, oneDestFile, listener, isMove)) return false;
246267
} else if (file.isDirectory()) {
247268
// 如果操作失败返回false
248-
if (!copyOrMoveDir(file, oneDestFile, isMove)) return false;
269+
if (!copyOrMoveDir(file, oneDestFile, listener, isMove)) return false;
249270
}
250271
}
251272
return !isMove || deleteDir(srcDir);
@@ -256,27 +277,38 @@ private static boolean copyOrMoveDir(final File srcDir, final File destDir, fina
256277
*
257278
* @param srcFilePath 源文件路径
258279
* @param destFilePath 目标文件路径
280+
* @param listener 是否覆盖监听器
259281
* @param isMove 是否移动
260282
* @return {@code true}: 复制或移动成功<br>{@code false}: 复制或移动失败
261283
*/
262-
private static boolean copyOrMoveFile(final String srcFilePath, final String destFilePath, final boolean isMove) {
263-
return copyOrMoveFile(getFileByPath(srcFilePath), getFileByPath(destFilePath), isMove);
284+
private static boolean copyOrMoveFile(final String srcFilePath, final String destFilePath, final OnReplaceListener listener, final boolean isMove) {
285+
return copyOrMoveFile(getFileByPath(srcFilePath), getFileByPath(destFilePath), listener, isMove);
264286
}
265287

266288
/**
267289
* 复制或移动文件
268290
*
269291
* @param srcFile 源文件
270292
* @param destFile 目标文件
293+
* @param listener 是否覆盖监听器
271294
* @param isMove 是否移动
272295
* @return {@code true}: 复制或移动成功<br>{@code false}: 复制或移动失败
273296
*/
274-
private static boolean copyOrMoveFile(final File srcFile, final File destFile, final boolean isMove) {
297+
private static boolean copyOrMoveFile(final File srcFile, final File destFile, final OnReplaceListener listener, final boolean isMove) {
275298
if (srcFile == null || destFile == null) return false;
299+
// 如果源文件和目标文件相同则返回false
300+
if (srcFile.equals(destFile)) return false;
276301
// 源文件不存在或者不是文件则返回false
277302
if (!srcFile.exists() || !srcFile.isFile()) return false;
278-
// 目标文件存在且是文件则返回false
279-
if (destFile.exists() && destFile.isFile()) return false;
303+
if (destFile.exists()) {// 目标文件存在
304+
if (listener.onReplace()) {// 需要覆盖则删除旧文件
305+
if (!destFile.delete()) {// 删除文件失败的话返回false
306+
return false;
307+
}
308+
} else {// 不需要覆盖直接返回即可true
309+
return true;
310+
}
311+
}
280312
// 目标目录不存在返回false
281313
if (!createOrExistsDir(destFile.getParentFile())) return false;
282314
try {
@@ -293,87 +325,95 @@ private static boolean copyOrMoveFile(final File srcFile, final File destFile, f
293325
*
294326
* @param srcDirPath 源目录路径
295327
* @param destDirPath 目标目录路径
328+
* @param listener 是否覆盖监听器
296329
* @return {@code true}: 复制成功<br>{@code false}: 复制失败
297330
*/
298-
public static boolean copyDir(final String srcDirPath, final String destDirPath) {
299-
return copyDir(getFileByPath(srcDirPath), getFileByPath(destDirPath));
331+
public static boolean copyDir(final String srcDirPath, final String destDirPath, final OnReplaceListener listener) {
332+
return copyDir(getFileByPath(srcDirPath), getFileByPath(destDirPath), listener);
300333
}
301334

302335
/**
303336
* 复制目录
304337
*
305-
* @param srcDir 源目录
306-
* @param destDir 目标目录
338+
* @param srcDir 源目录
339+
* @param destDir 目标目录
340+
* @param listener 是否覆盖监听器
307341
* @return {@code true}: 复制成功<br>{@code false}: 复制失败
308342
*/
309-
public static boolean copyDir(final File srcDir, final File destDir) {
310-
return copyOrMoveDir(srcDir, destDir, false);
343+
public static boolean copyDir(final File srcDir, final File destDir, final OnReplaceListener listener) {
344+
return copyOrMoveDir(srcDir, destDir, listener, false);
311345
}
312346

313347
/**
314348
* 复制文件
315349
*
316350
* @param srcFilePath 源文件路径
317351
* @param destFilePath 目标文件路径
352+
* @param listener 是否覆盖监听器
318353
* @return {@code true}: 复制成功<br>{@code false}: 复制失败
319354
*/
320-
public static boolean copyFile(final String srcFilePath, final String destFilePath) {
321-
return copyFile(getFileByPath(srcFilePath), getFileByPath(destFilePath));
355+
public static boolean copyFile(final String srcFilePath, final String destFilePath, final OnReplaceListener listener) {
356+
return copyFile(getFileByPath(srcFilePath), getFileByPath(destFilePath), listener);
322357
}
323358

324359
/**
325360
* 复制文件
326361
*
327362
* @param srcFile 源文件
328363
* @param destFile 目标文件
364+
* @param listener 是否覆盖监听器
329365
* @return {@code true}: 复制成功<br>{@code false}: 复制失败
330366
*/
331-
public static boolean copyFile(final File srcFile, final File destFile) {
332-
return copyOrMoveFile(srcFile, destFile, false);
367+
public static boolean copyFile(final File srcFile, final File destFile, final OnReplaceListener listener) {
368+
return copyOrMoveFile(srcFile, destFile, listener, false);
333369
}
334370

335371
/**
336372
* 移动目录
337373
*
338374
* @param srcDirPath 源目录路径
339375
* @param destDirPath 目标目录路径
376+
* @param listener 是否覆盖监听器
340377
* @return {@code true}: 移动成功<br>{@code false}: 移动失败
341378
*/
342-
public static boolean moveDir(final String srcDirPath, final String destDirPath) {
343-
return moveDir(getFileByPath(srcDirPath), getFileByPath(destDirPath));
379+
public static boolean moveDir(final String srcDirPath, final String destDirPath, final OnReplaceListener listener) {
380+
return moveDir(getFileByPath(srcDirPath), getFileByPath(destDirPath), listener);
344381
}
345382

346383
/**
347384
* 移动目录
348385
*
349-
* @param srcDir 源目录
350-
* @param destDir 目标目录
386+
* @param srcDir 源目录
387+
* @param destDir 目标目录
388+
* @param listener 是否覆盖监听器
351389
* @return {@code true}: 移动成功<br>{@code false}: 移动失败
352390
*/
353-
public static boolean moveDir(final File srcDir, final File destDir) {
354-
return copyOrMoveDir(srcDir, destDir, true);
391+
public static boolean moveDir(final File srcDir, final File destDir, final OnReplaceListener listener) {
392+
return copyOrMoveDir(srcDir, destDir, listener, true);
355393
}
356394

357395
/**
358396
* 移动文件
359397
*
360398
* @param srcFilePath 源文件路径
361399
* @param destFilePath 目标文件路径
400+
* @param listener 是否覆盖监听器
362401
* @return {@code true}: 移动成功<br>{@code false}: 移动失败
363402
*/
364-
public static boolean moveFile(final String srcFilePath, final String destFilePath) {
365-
return moveFile(getFileByPath(srcFilePath), getFileByPath(destFilePath));
403+
public static boolean moveFile(final String srcFilePath, final String destFilePath, final OnReplaceListener listener) {
404+
return moveFile(getFileByPath(srcFilePath), getFileByPath(destFilePath), listener);
366405
}
367406

368407
/**
369408
* 移动文件
370409
*
371410
* @param srcFile 源文件
372411
* @param destFile 目标文件
412+
* @param listener 是否覆盖监听器
373413
* @return {@code true}: 移动成功<br>{@code false}: 移动失败
374414
*/
375-
public static boolean moveFile(final File srcFile, final File destFile) {
376-
return copyOrMoveFile(srcFile, destFile, true);
415+
public static boolean moveFile(final File srcFile, final File destFile, final OnReplaceListener listener) {
416+
return copyOrMoveFile(srcFile, destFile, listener, true);
377417
}
378418

379419
/**
@@ -524,7 +564,7 @@ public static boolean deleteFilesInDirWithFilter(final File dir, final FileFilte
524564

525565
/**
526566
* 获取目录下所有文件
527-
* <p>不递归进子目录</p>
567+
* <p>不递归进子目录</p>
528568
*
529569
* @param dirPath 目录路径
530570
* @return 文件链表
@@ -535,7 +575,7 @@ public static List<File> listFilesInDir(final String dirPath) {
535575

536576
/**
537577
* 获取目录下所有文件
538-
* <p>不递归进子目录</p>
578+
* <p>不递归进子目录</p>
539579
*
540580
* @param dir 目录
541581
* @return 文件链表
@@ -854,22 +894,21 @@ public static String getFileMD5ToString(final String filePath) {
854894
/**
855895
* 获取文件的MD5校验码
856896
*
857-
* @param filePath 文件路径
897+
* @param file 文件
858898
* @return 文件的MD5校验码
859899
*/
860-
public static byte[] getFileMD5(final String filePath) {
861-
File file = isSpace(filePath) ? null : new File(filePath);
862-
return getFileMD5(file);
900+
public static String getFileMD5ToString(final File file) {
901+
return bytes2HexString(getFileMD5(file));
863902
}
864903

865904
/**
866905
* 获取文件的MD5校验码
867906
*
868-
* @param file 文件
907+
* @param filePath 文件路径
869908
* @return 文件的MD5校验码
870909
*/
871-
public static String getFileMD5ToString(final File file) {
872-
return bytes2HexString(getFileMD5(file));
910+
public static byte[] getFileMD5(final String filePath) {
911+
return getFileMD5(getFileByPath(filePath));
873912
}
874913

875914
/**
@@ -1057,4 +1096,8 @@ private static boolean isSpace(final String s) {
10571096
}
10581097
return true;
10591098
}
1099+
1100+
public interface OnReplaceListener {
1101+
boolean onReplace();
1102+
}
10601103
}

0 commit comments

Comments
 (0)