@@ -34,9 +34,11 @@ private FileUtils() {
34
34
}
35
35
36
36
/**
37
+ * 关闭IO
38
+ *
37
39
* @param closeable closeable
38
40
*/
39
- private static void closeIO (Closeable closeable ) {
41
+ public static void closeIO (Closeable closeable ) {
40
42
if (closeable == null ) return ;
41
43
try {
42
44
closeable .close ();
@@ -132,7 +134,8 @@ public static boolean createOrExistsDir(String dirPath) {
132
134
* @return {@code true}: 存在或创建成功<br>{@code false}: 不存在或创建失败
133
135
*/
134
136
public static boolean createOrExistsDir (File file ) {
135
- return file != null && (file .exists () && file .isDirectory () || file .mkdirs ());
137
+ // 如果存在,是目录则返回true,是文件则返回false,不存在则返回是否创建成功
138
+ return file != null && (file .exists () ? file .isDirectory () : file .mkdirs ());
136
139
}
137
140
138
141
/**
@@ -153,7 +156,8 @@ public static boolean createOrExistsFile(String filePath) {
153
156
*/
154
157
public static boolean createOrExistsFile (File file ) {
155
158
if (file == null ) return false ;
156
- if (file .exists () && file .isFile ()) return true ;
159
+ // 如果存在,是文件则返回true,是目录则返回false
160
+ if (file .exists ()) return file .isFile ();
157
161
if (!createOrExistsDir (file .getParentFile ())) return false ;
158
162
try {
159
163
return file .createNewFile ();
@@ -215,22 +219,29 @@ private static boolean copyOrMoveDir(String srcDirPath, String destDirPath, bool
215
219
*/
216
220
private static boolean copyOrMoveDir (File srcDir , File destDir , boolean isMove ) {
217
221
if (srcDir == null || destDir == null ) return false ;
222
+ // 如果目标目录在源目录中则返回false,看不懂的话好好想想递归怎么结束
223
+ // srcPath : F:\\MyGithub\\AndroidUtilCode\\utilcode\\src\\test\\res
224
+ // destPath: F:\\MyGithub\\AndroidUtilCode\\utilcode\\src\\test\\res1
225
+ // 为防止以上这种情况出现出现误判,须分别在后面加个路径分隔符
226
+ String srcPath = srcDir .getPath () + File .separator ;
227
+ String destPath = destDir .getPath () + File .separator ;
228
+ if (destPath .contains (srcPath )) return false ;
218
229
// 源文件不存在或者不是目录则返回false
219
230
if (!srcDir .exists () || !srcDir .isDirectory ()) return false ;
220
231
// 目标目录不存在返回false
221
232
if (!createOrExistsDir (destDir )) return false ;
222
- File [] srcDirFiles = srcDir .listFiles ();
223
- for (File srcDirFile : srcDirFiles ) {
224
- File oneDestFile = new File (destDir .getPath () + File .separator
225
- + srcDirFile .getName ());
226
- if (srcDirFile .isFile ()) {
227
- copyOrMoveFile (srcDirFile , oneDestFile , isMove );
228
- } else if (srcDirFile .isDirectory ()) {
229
- copyOrMoveDir (srcDirFile , oneDestFile , isMove );
233
+ File [] files = srcDir .listFiles ();
234
+ for (File file : files ) {
235
+ File oneDestFile = new File (destPath + file .getName ());
236
+ if (file .isFile ()) {
237
+ // 如果操作失败返回false
238
+ if (!copyOrMoveFile (file , oneDestFile , isMove )) return false ;
239
+ } else if (file .isDirectory ()) {
240
+ // 如果操作失败返回false
241
+ if (!copyOrMoveDir (file , oneDestFile , isMove )) return false ;
230
242
}
231
- if (isMove && !deleteFile (srcDirFile )) return false ;
232
243
}
233
- return true ;
244
+ return ! isMove || deleteDir ( srcDir ) ;
234
245
}
235
246
236
247
/**
@@ -262,7 +273,8 @@ private static boolean copyOrMoveFile(File srcFile, File destFile, boolean isMov
262
273
// 目标目录不存在返回false
263
274
if (!createOrExistsDir (destFile .getParentFile ())) return false ;
264
275
try {
265
- return writeFileFromIS (destFile , new FileInputStream (srcFile ), false ) && !(isMove && !deleteFile (srcFile ));
276
+ return writeFileFromIS (destFile , new FileInputStream (srcFile ), false )
277
+ && !(isMove && !deleteFile (srcFile ));
266
278
} catch (FileNotFoundException e ) {
267
279
e .printStackTrace ();
268
280
return false ;
@@ -276,8 +288,8 @@ private static boolean copyOrMoveFile(File srcFile, File destFile, boolean isMov
276
288
* @param destDirPath 目标目录路径
277
289
* @return {@code true}: 复制成功<br>{@code false}: 复制失败
278
290
*/
279
- public boolean copyDir (String srcDirPath , String destDirPath ) {
280
- return moveDir (getFileByPath (srcDirPath ), getFileByPath (destDirPath ));
291
+ public static boolean copyDir (String srcDirPath , String destDirPath ) {
292
+ return copyDir (getFileByPath (srcDirPath ), getFileByPath (destDirPath ));
281
293
}
282
294
283
295
/**
@@ -296,7 +308,7 @@ public static boolean copyDir(File srcDir, File destDir) {
296
308
*
297
309
* @param srcFilePath 源文件路径
298
310
* @param destFilePath 目标文件路径
299
- * @return {@code true}: 复制拷贝成功 <br>{@code false}: 复制失败
311
+ * @return {@code true}: 复制成功 <br>{@code false}: 复制失败
300
312
*/
301
313
public static boolean copyFile (String srcFilePath , String destFilePath ) {
302
314
return copyFile (getFileByPath (srcFilePath ), getFileByPath (destFilePath ));
@@ -320,7 +332,7 @@ public static boolean copyFile(File srcFile, File destFile) {
320
332
* @param destDirPath 目标目录路径
321
333
* @return {@code true}: 移动成功<br>{@code false}: 移动失败
322
334
*/
323
- public boolean moveDir (String srcDirPath , String destDirPath ) {
335
+ public static boolean moveDir (String srcDirPath , String destDirPath ) {
324
336
return moveDir (getFileByPath (srcDirPath ), getFileByPath (destDirPath ));
325
337
}
326
338
@@ -363,7 +375,7 @@ public static boolean moveFile(File srcFile, File destFile) {
363
375
* @param dirPath 目录路径
364
376
* @return {@code true}: 删除成功<br>{@code false}: 删除失败
365
377
*/
366
- public boolean deleteDir (String dirPath ) {
378
+ public static boolean deleteDir (String dirPath ) {
367
379
return deleteDir (getFileByPath (dirPath ));
368
380
}
369
381
@@ -373,9 +385,11 @@ public boolean deleteDir(String dirPath) {
373
385
* @param dir 目录
374
386
* @return {@code true}: 删除成功<br>{@code false}: 删除失败
375
387
*/
376
- public boolean deleteDir (File dir ) {
388
+ public static boolean deleteDir (File dir ) {
377
389
if (dir == null ) return false ;
390
+ // 目录不存在返回true
378
391
if (!dir .exists ()) return true ;
392
+ // 不是目录返回false
379
393
if (!dir .isDirectory ()) return false ;
380
394
// 现在文件存在且是文件夹
381
395
File [] files = dir .listFiles ();
@@ -436,7 +450,10 @@ public static boolean writeFileFromIS(File file, InputStream is, boolean append)
436
450
try {
437
451
os = new BufferedOutputStream (new FileOutputStream (file , append ));
438
452
byte data [] = new byte [KB ];
439
- while (is .read (data ) != -1 ) os .write (data );
453
+ int len ;
454
+ while ((len = is .read (data )) != -1 ) {
455
+ os .write (data , 0 , len );
456
+ }
440
457
return true ;
441
458
} catch (IOException e ) {
442
459
e .printStackTrace ();
@@ -589,7 +606,8 @@ public static List<String> readFile2List(File file, String charsetName) {
589
606
* @param charsetName 编码格式
590
607
* @return 包含制定行的list
591
608
*/
592
- public static List <String > readFile2List (String filePath , int start , int end , String charsetName ) {
609
+ public static List <String > readFile2List (String filePath , int start , int end , String
610
+ charsetName ) {
593
611
return readFile2List (getFileByPath (filePath ), start , end , charsetName );
594
612
}
595
613
@@ -653,7 +671,12 @@ public static StringBuilder readFile2SB(File file, String charsetName) {
653
671
BufferedReader reader = null ;
654
672
try {
655
673
sb = new StringBuilder ();
656
- reader = new BufferedReader (new InputStreamReader (new FileInputStream (file ), charsetName ));
674
+ if (StringUtils .isSpace (charsetName )) {
675
+ reader = new BufferedReader (new FileReader (file ));
676
+ } else {
677
+ reader = new BufferedReader (new InputStreamReader (new FileInputStream (file ),
678
+ charsetName ));
679
+ }
657
680
String line ;
658
681
while ((line = reader .readLine ()) != null ) {
659
682
sb .append (line );
0 commit comments