@@ -111,7 +111,7 @@ public static boolean isDir(final String dirPath) {
111
111
* @return {@code true}: 是<br>{@code false}: 否
112
112
*/
113
113
public static boolean isDir (final File file ) {
114
- return isFileExists ( file ) && file .isDirectory ();
114
+ return file != null && file . exists ( ) && file .isDirectory ();
115
115
}
116
116
117
117
/**
@@ -131,7 +131,7 @@ public static boolean isFile(final String filePath) {
131
131
* @return {@code true}: 是<br>{@code false}: 否
132
132
*/
133
133
public static boolean isFile (final File file ) {
134
- return isFileExists ( file ) && file .isFile ();
134
+ return file != null && file . exists ( ) && file .isFile ();
135
135
}
136
136
137
137
/**
@@ -184,6 +184,16 @@ public static boolean createOrExistsFile(final File file) {
184
184
}
185
185
}
186
186
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
+
187
197
/**
188
198
* 判断文件是否存在,存在则在创建之前删除
189
199
*
@@ -209,22 +219,24 @@ public static boolean createFileByDeleteOldFile(final File file) {
209
219
*
210
220
* @param srcDirPath 源目录路径
211
221
* @param destDirPath 目标目录路径
222
+ * @param listener 是否覆盖监听器
212
223
* @param isMove 是否移动
213
224
* @return {@code true}: 复制或移动成功<br>{@code false}: 复制或移动失败
214
225
*/
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 );
217
228
}
218
229
219
230
/**
220
231
* 复制或移动目录
221
232
*
222
- * @param srcDir 源目录
223
- * @param destDir 目标目录
224
- * @param isMove 是否移动
233
+ * @param srcDir 源目录
234
+ * @param destDir 目标目录
235
+ * @param listener 是否覆盖监听器
236
+ * @param isMove 是否移动
225
237
* @return {@code true}: 复制或移动成功<br>{@code false}: 复制或移动失败
226
238
*/
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 ) {
228
240
if (srcDir == null || destDir == null ) return false ;
229
241
// 如果目标目录在源目录中则返回false,看不懂的话好好想想递归怎么结束
230
242
// srcPath : F:\\MyGithub\\AndroidUtilCode\\utilcode\\src\\test\\res
@@ -235,17 +247,26 @@ private static boolean copyOrMoveDir(final File srcDir, final File destDir, fina
235
247
if (destPath .contains (srcPath )) return false ;
236
248
// 源文件不存在或者不是目录则返回false
237
249
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
+ }
238
259
// 目标目录不存在返回false
239
260
if (!createOrExistsDir (destDir )) return false ;
240
261
File [] files = srcDir .listFiles ();
241
262
for (File file : files ) {
242
263
File oneDestFile = new File (destPath + file .getName ());
243
264
if (file .isFile ()) {
244
265
// 如果操作失败返回false
245
- if (!copyOrMoveFile (file , oneDestFile , isMove )) return false ;
266
+ if (!copyOrMoveFile (file , oneDestFile , listener , isMove )) return false ;
246
267
} else if (file .isDirectory ()) {
247
268
// 如果操作失败返回false
248
- if (!copyOrMoveDir (file , oneDestFile , isMove )) return false ;
269
+ if (!copyOrMoveDir (file , oneDestFile , listener , isMove )) return false ;
249
270
}
250
271
}
251
272
return !isMove || deleteDir (srcDir );
@@ -256,27 +277,38 @@ private static boolean copyOrMoveDir(final File srcDir, final File destDir, fina
256
277
*
257
278
* @param srcFilePath 源文件路径
258
279
* @param destFilePath 目标文件路径
280
+ * @param listener 是否覆盖监听器
259
281
* @param isMove 是否移动
260
282
* @return {@code true}: 复制或移动成功<br>{@code false}: 复制或移动失败
261
283
*/
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 );
264
286
}
265
287
266
288
/**
267
289
* 复制或移动文件
268
290
*
269
291
* @param srcFile 源文件
270
292
* @param destFile 目标文件
293
+ * @param listener 是否覆盖监听器
271
294
* @param isMove 是否移动
272
295
* @return {@code true}: 复制或移动成功<br>{@code false}: 复制或移动失败
273
296
*/
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 ) {
275
298
if (srcFile == null || destFile == null ) return false ;
299
+ // 如果源文件和目标文件相同则返回false
300
+ if (srcFile .equals (destFile )) return false ;
276
301
// 源文件不存在或者不是文件则返回false
277
302
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
+ }
280
312
// 目标目录不存在返回false
281
313
if (!createOrExistsDir (destFile .getParentFile ())) return false ;
282
314
try {
@@ -293,87 +325,95 @@ private static boolean copyOrMoveFile(final File srcFile, final File destFile, f
293
325
*
294
326
* @param srcDirPath 源目录路径
295
327
* @param destDirPath 目标目录路径
328
+ * @param listener 是否覆盖监听器
296
329
* @return {@code true}: 复制成功<br>{@code false}: 复制失败
297
330
*/
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 );
300
333
}
301
334
302
335
/**
303
336
* 复制目录
304
337
*
305
- * @param srcDir 源目录
306
- * @param destDir 目标目录
338
+ * @param srcDir 源目录
339
+ * @param destDir 目标目录
340
+ * @param listener 是否覆盖监听器
307
341
* @return {@code true}: 复制成功<br>{@code false}: 复制失败
308
342
*/
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 );
311
345
}
312
346
313
347
/**
314
348
* 复制文件
315
349
*
316
350
* @param srcFilePath 源文件路径
317
351
* @param destFilePath 目标文件路径
352
+ * @param listener 是否覆盖监听器
318
353
* @return {@code true}: 复制成功<br>{@code false}: 复制失败
319
354
*/
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 );
322
357
}
323
358
324
359
/**
325
360
* 复制文件
326
361
*
327
362
* @param srcFile 源文件
328
363
* @param destFile 目标文件
364
+ * @param listener 是否覆盖监听器
329
365
* @return {@code true}: 复制成功<br>{@code false}: 复制失败
330
366
*/
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 );
333
369
}
334
370
335
371
/**
336
372
* 移动目录
337
373
*
338
374
* @param srcDirPath 源目录路径
339
375
* @param destDirPath 目标目录路径
376
+ * @param listener 是否覆盖监听器
340
377
* @return {@code true}: 移动成功<br>{@code false}: 移动失败
341
378
*/
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 );
344
381
}
345
382
346
383
/**
347
384
* 移动目录
348
385
*
349
- * @param srcDir 源目录
350
- * @param destDir 目标目录
386
+ * @param srcDir 源目录
387
+ * @param destDir 目标目录
388
+ * @param listener 是否覆盖监听器
351
389
* @return {@code true}: 移动成功<br>{@code false}: 移动失败
352
390
*/
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 );
355
393
}
356
394
357
395
/**
358
396
* 移动文件
359
397
*
360
398
* @param srcFilePath 源文件路径
361
399
* @param destFilePath 目标文件路径
400
+ * @param listener 是否覆盖监听器
362
401
* @return {@code true}: 移动成功<br>{@code false}: 移动失败
363
402
*/
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 );
366
405
}
367
406
368
407
/**
369
408
* 移动文件
370
409
*
371
410
* @param srcFile 源文件
372
411
* @param destFile 目标文件
412
+ * @param listener 是否覆盖监听器
373
413
* @return {@code true}: 移动成功<br>{@code false}: 移动失败
374
414
*/
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 );
377
417
}
378
418
379
419
/**
@@ -524,7 +564,7 @@ public static boolean deleteFilesInDirWithFilter(final File dir, final FileFilte
524
564
525
565
/**
526
566
* 获取目录下所有文件
527
- * <p>不递归进子目录</p>
567
+ * <p>不递归进子目录</p>
528
568
*
529
569
* @param dirPath 目录路径
530
570
* @return 文件链表
@@ -535,7 +575,7 @@ public static List<File> listFilesInDir(final String dirPath) {
535
575
536
576
/**
537
577
* 获取目录下所有文件
538
- * <p>不递归进子目录</p>
578
+ * <p>不递归进子目录</p>
539
579
*
540
580
* @param dir 目录
541
581
* @return 文件链表
@@ -854,22 +894,21 @@ public static String getFileMD5ToString(final String filePath) {
854
894
/**
855
895
* 获取文件的MD5校验码
856
896
*
857
- * @param filePath 文件路径
897
+ * @param file 文件
858
898
* @return 文件的MD5校验码
859
899
*/
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 ));
863
902
}
864
903
865
904
/**
866
905
* 获取文件的MD5校验码
867
906
*
868
- * @param file 文件
907
+ * @param filePath 文件路径
869
908
* @return 文件的MD5校验码
870
909
*/
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 ));
873
912
}
874
913
875
914
/**
@@ -1057,4 +1096,8 @@ private static boolean isSpace(final String s) {
1057
1096
}
1058
1097
return true ;
1059
1098
}
1099
+
1100
+ public interface OnReplaceListener {
1101
+ boolean onReplace ();
1102
+ }
1060
1103
}
0 commit comments