-
Notifications
You must be signed in to change notification settings - Fork 31
/
FileUtils.java
823 lines (723 loc) · 29.3 KB
/
FileUtils.java
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
package me.shouheng.utils.store;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build.VERSION_CODES;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import me.shouheng.utils.UtilsApp;
import me.shouheng.utils.data.StringUtils;
/**
* 用来处理文件的拷贝、增加和删除等逻辑
*
* @author Shouheng Wang 2019-05-08 21:30
*/
public final class FileUtils {
private static final String LINE_SEP = System.getProperty("line.separator");
/*------------------------------------- 查询方法 ----------------------------------------*/
public static boolean isStorageReadable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}
public static boolean isStorageWritable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}
@RequiresApi(api = VERSION_CODES.KITKAT)
public static String getFilePath(@NonNull final Uri uri) {
Context context = UtilsApp.getApp();
if (DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
} else if (isDownloadsDocument(uri)) {
final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
Long.parseLong(DocumentsContract.getDocumentId(uri)));
return getDataColumn(context, contentUri, null, null);
} else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = createURI(type);
final String selection = "_id=?";
final String[] selectionArgs = new String[]{split[1]};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
public static File getFileByPath(final String filePath) {
return StringUtils.isSpace(filePath) ? null : new File(filePath);
}
public static boolean isFileExists(final String filePath) {
return isFileExists(getFileByPath(filePath));
}
public static boolean isFileExists(final File file) {
return file != null && file.exists();
}
public static boolean isFile(final String filePath) {
return isFile(getFileByPath(filePath));
}
public static boolean isFile(final File file) {
return file != null && file.exists() && file.isFile();
}
public static boolean isDir(final String dirPath) {
return isDir(getFileByPath(dirPath));
}
public static boolean isDir(final File file) {
return file != null && file.exists() && file.isDirectory();
}
public static String getFileName(@NonNull final Uri uri) {
String fileName = "";
Cursor cursor = null;
try {
cursor = UtilsApp.getApp().getContentResolver().query(
uri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
} else {
fileName = uri.getLastPathSegment();
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
return fileName;
}
public static String getFileName(final File file) {
if (file == null) return "";
return getFileName(file.getAbsolutePath());
}
public static String getFileName(final String filePath) {
if (StringUtils.isSpace(filePath)) return "";
int lastSep = filePath.lastIndexOf(File.separator);
return lastSep == -1 ? filePath : filePath.substring(lastSep + 1);
}
public static String getFileNameNoExtension(final File file) {
if (file == null) return "";
return getFileNameNoExtension(file.getPath());
}
public static String getFileNameNoExtension(final String filePath) {
if (StringUtils.isSpace(filePath)) return "";
int lastPoi = filePath.lastIndexOf('.');
int lastSep = filePath.lastIndexOf(File.separator);
if (lastSep == -1) {
return (lastPoi == -1 ? filePath : filePath.substring(0, lastPoi));
}
if (lastPoi == -1 || lastSep > lastPoi) {
return filePath.substring(lastSep + 1);
}
return filePath.substring(lastSep + 1, lastPoi);
}
public static String getFileExtension(final File file) {
if (file == null) return "";
return getFileExtension(file.getPath());
}
public static String getFileExtension(final String filePath) {
if (StringUtils.isSpace(filePath)) return "";
int lastPoi = filePath.lastIndexOf('.');
int lastSep = filePath.lastIndexOf(File.separator);
if (lastPoi == -1 || lastSep >= lastPoi) return "";
return filePath.substring(lastPoi + 1);
}
public static String getDirName(final File file) {
if (file == null) return "";
return getDirName(file.getAbsolutePath());
}
public static String getDirName(final String filePath) {
if (StringUtils.isSpace(filePath)) return "";
int lastSep = filePath.lastIndexOf(File.separator);
return lastSep == -1 ? "" : filePath.substring(0, lastSep + 1);
}
public static long getFileLastModified(final String filePath) {
return getFileLastModified(getFileByPath(filePath));
}
public static long getFileLastModified(final File file) {
if (file == null) return -1;
return file.lastModified();
}
public static String getFileCharsetSimple(final String filePath) {
return getFileCharsetSimple(getFileByPath(filePath));
}
public static String getFileCharsetSimple(final File file) {
int p = 0;
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(file));
p = (is.read() << 8) + is.read();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
switch (p) {
case 0xefbb:
return "UTF-8";
case 0xfffe:
return "Unicode";
case 0xfeff:
return "UTF-16BE";
default:
return "GBK";
}
}
public static int getFileLines(final String filePath) {
return getFileLines(getFileByPath(filePath));
}
public static int getFileLines(final File file) {
int count = 1;
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
int readChars;
if (LINE_SEP.endsWith("\n")) {
while ((readChars = is.read(buffer, 0, 1024)) != -1) {
for (int i = 0; i < readChars; ++i) {
if (buffer[i] == '\n') ++count;
}
}
} else {
while ((readChars = is.read(buffer, 0, 1024)) != -1) {
for (int i = 0; i < readChars; ++i) {
if (buffer[i] == '\r') ++count;
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return count;
}
public static String getDirSize(final String dirPath) {
return getDirSize(getFileByPath(dirPath));
}
public static String getDirSize(final File dir) {
long len = getDirLength(dir);
return len == -1 ? "" : byte2FitMemorySize(len);
}
public static String getFileSize(final String filePath) {
long len = getFileLength(filePath);
return len == -1 ? "" : byte2FitMemorySize(len);
}
public static String getFileSize(final File file) {
long len = getFileLength(file);
return len == -1 ? "" : byte2FitMemorySize(len);
}
public static long getDirLength(final String dirPath) {
return getDirLength(getFileByPath(dirPath));
}
public static long getDirLength(final File dir) {
if (!isDir(dir)) return -1;
long len = 0;
File[] files = dir.listFiles();
if (files != null && files.length != 0) {
for (File file : files) {
if (file.isDirectory()) {
len += getDirLength(file);
} else {
len += file.length();
}
}
}
return len;
}
public static long getFileLength(final String filePath) {
boolean isURL = filePath.matches("[a-zA-z]+://[^\\s]*");
if (isURL) {
try {
HttpURLConnection conn = (HttpURLConnection) new URL(filePath).openConnection();
conn.setRequestProperty("Accept-Encoding", "identity");
conn.connect();
if (conn.getResponseCode() == 200) {
return conn.getContentLength();
}
return -1;
} catch (IOException e) {
e.printStackTrace();
}
}
return getFileLength(getFileByPath(filePath));
}
public static long getFileLength(final File file) {
if (!isFile(file)) return -1;
return file.length();
}
public static String getFileMD5ToString(final String filePath) {
File file = StringUtils.isSpace(filePath) ? null : new File(filePath);
return getFileMD5ToString(file);
}
public static String getFileMD5ToString(final File file) {
return StringUtils.bytes2HexString(getFileMD5(file));
}
public static byte[] getFileMD5(final String filePath) {
return getFileMD5(getFileByPath(filePath));
}
public static byte[] getFileMD5(final File file) {
if (file == null) return null;
DigestInputStream dis = null;
try {
FileInputStream fis = new FileInputStream(file);
MessageDigest md = MessageDigest.getInstance("MD5");
dis = new DigestInputStream(fis, md);
byte[] buffer = new byte[1024 * 256];
while (true) {
if (!(dis.read(buffer) > 0)) break;
}
md = dis.getMessageDigest();
return md.digest();
} catch (NoSuchAlgorithmException | IOException e) {
e.printStackTrace();
} finally {
try {
if (dis != null) {
dis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static List<File> listFilesInDir(final String dirPath) {
return listFilesInDir(dirPath, false);
}
public static List<File> listFilesInDir(final File dir) {
return listFilesInDir(dir, false);
}
public static List<File> listFilesInDir(final String dirPath, final boolean isRecursive) {
return listFilesInDir(getFileByPath(dirPath), isRecursive);
}
public static List<File> listFilesInDir(final File dir, final boolean isRecursive) {
return listFilesInDirWithFilter(dir, new FileFilter() {
@Override
public boolean accept(File pathname) {
return true;
}
}, isRecursive);
}
public static List<File> listFilesInDirWithFilter(final String dirPath, final FileFilter filter) {
return listFilesInDirWithFilter(getFileByPath(dirPath), filter, false);
}
public static List<File> listFilesInDirWithFilter(final File dir, final FileFilter filter) {
return listFilesInDirWithFilter(dir, filter, false);
}
public static List<File> listFilesInDirWithFilter(final String dirPath, final FileFilter filter, final boolean isRecursive) {
return listFilesInDirWithFilter(getFileByPath(dirPath), filter, isRecursive);
}
public static List<File> listFilesInDirWithFilter(final File dir, final FileFilter filter, final boolean isRecursive) {
if (!isDir(dir)) return null;
List<File> list = new ArrayList<>();
File[] files = dir.listFiles();
if (files != null && files.length != 0) {
for (File file : files) {
if (filter.accept(file)) {
list.add(file);
}
if (isRecursive && file.isDirectory()) {
//noinspection ConstantConditions
list.addAll(listFilesInDirWithFilter(file, filter, true));
}
}
}
return list;
}
/*------------------------------------- 增删改方法 ----------------------------------------*/
public static boolean createOrExistsDir(final String dirPath) {
return createOrExistsDir(getFileByPath(dirPath));
}
/**
* 判断指定的文件是否存在,如果不存在就创建它
*
* @param file 指定的文件
* @return 该文件是否存在,true 表示存在
*/
public static boolean createOrExistsFile(final File file) {
if (file == null) return false;
if (file.exists()) return file.isFile();
// 判断父目录是否是文件夹,如果不存在就创建它
if (!createOrExistsDir(file.getParentFile())) return false;
try {
// 创建文件
return file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static boolean createFileByDeleteOldFile(final String filePath) {
return createFileByDeleteOldFile(getFileByPath(filePath));
}
/**
* Create a file if it doesn't exist, otherwise delete old file before creating.
*
* @param file The file.
* @return {@code true}: success<br>{@code false}: fail
*/
public static boolean createFileByDeleteOldFile(final File file) {
if (file == null) return false;
// file exists and unsuccessfully delete then return false
if (file.exists() && !file.delete()) return false;
if (!createOrExistsDir(file.getParentFile())) return false;
try {
return file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static boolean rename(final File file, final String newName) {
// file is null then return false
if (file == null) return false;
// file doesn't exist then return false
if (!file.exists()) return false;
// the new name is space then return false
if (StringUtils.isSpace(newName)) return false;
// the new name equals old name then return true
if (newName.equals(file.getName())) return true;
File newFile = new File(file.getParent() + File.separator + newName);
// the new name of file exists then return false
return !newFile.exists() && file.renameTo(newFile);
}
public static boolean copyFile(final String srcFilePath, final String destFilePath) {
return copyFile(getFileByPath(srcFilePath), getFileByPath(destFilePath));
}
public static boolean copyFile(final String srcFilePath, final String destFilePath, final OnReplaceListener listener) {
return copyFile(getFileByPath(srcFilePath), getFileByPath(destFilePath), listener);
}
public static boolean copyFile(final File srcFile, final File destFile) {
return copyOrMoveFile(srcFile, destFile, false);
}
public static boolean copyFile(final File srcFile, final File destFile, final OnReplaceListener listener) {
return copyOrMoveFile(srcFile, destFile, listener, false);
}
public static boolean moveFile(final String srcFilePath, final String destFilePath) {
return moveFile(getFileByPath(srcFilePath), getFileByPath(destFilePath));
}
public static boolean moveFile(final String srcFilePath, final String destFilePath, final OnReplaceListener listener) {
return moveFile(getFileByPath(srcFilePath), getFileByPath(destFilePath), listener);
}
public static boolean moveFile(final File srcFile, final File destFile) {
return copyOrMoveFile(srcFile, destFile, true);
}
public static boolean moveFile(final File srcFile, final File destFile, final OnReplaceListener listener) {
return copyOrMoveFile(srcFile, destFile, listener, true);
}
public static boolean copyDir(final String srcDirPath, final String destDirPath) {
return copyDir(getFileByPath(srcDirPath), getFileByPath(destDirPath));
}
public static boolean copyDir(final String srcDirPath, final String destDirPath, final OnReplaceListener listener) {
return copyDir(getFileByPath(srcDirPath), getFileByPath(destDirPath), listener);
}
public static boolean copyDir(final File srcDir, final File destDir) {
return copyOrMoveDir(srcDir, destDir, false);
}
public static boolean copyDir(final File srcDir, final File destDir, final OnReplaceListener listener) {
return copyOrMoveDir(srcDir, destDir, listener, false);
}
public static boolean moveDir(final String srcDirPath, final String destDirPath) {
return moveDir(getFileByPath(srcDirPath), getFileByPath(destDirPath));
}
public static boolean moveDir(final String srcDirPath, final String destDirPath, final OnReplaceListener listener) {
return moveDir(getFileByPath(srcDirPath), getFileByPath(destDirPath), listener);
}
public static boolean moveDir(final File srcDir, final File destDir) {
return copyOrMoveDir(srcDir, destDir, true);
}
public static boolean moveDir(final File srcDir, final File destDir, final OnReplaceListener listener) {
return copyOrMoveDir(srcDir, destDir, listener, true);
}
public static boolean delete(final String filePath) {
return delete(getFileByPath(filePath));
}
public static boolean delete(final File file) {
if (file == null) return false;
if (file.isDirectory()) {
return deleteDir(file);
}
return deleteFile(file);
}
public static boolean deleteDir(final String dirPath) {
return deleteDir(getFileByPath(dirPath));
}
public static boolean deleteDir(final File dir) {
if (dir == null) return false;
// dir doesn't exist then return true
if (!dir.exists()) return true;
// dir isn't a directory then return false
if (!dir.isDirectory()) return false;
File[] files = dir.listFiles();
if (files != null && files.length != 0) {
for (File file : files) {
if (file.isFile()) {
if (!file.delete()) return false;
} else if (file.isDirectory()) {
if (!deleteDir(file)) return false;
}
}
}
return dir.delete();
}
public static boolean deleteFile(final String srcFilePath) {
return deleteFile(getFileByPath(srcFilePath));
}
public static boolean deleteFile(final File file) {
return file != null && (!file.exists() || file.isFile() && file.delete());
}
public static boolean deleteAllInDir(final String dirPath) {
return deleteAllInDir(getFileByPath(dirPath));
}
public static boolean deleteAllInDir(final File dir) {
return deleteFilesInDirWithFilter(dir, new FileFilter() {
@Override
public boolean accept(File pathname) {
return true;
}
});
}
public static boolean deleteFilesInDir(final String dirPath) {
return deleteFilesInDir(getFileByPath(dirPath));
}
public static boolean deleteFilesInDir(final File dir) {
return deleteFilesInDirWithFilter(dir, new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isFile();
}
});
}
public static boolean deleteFilesInDirWithFilter(final String dirPath, final FileFilter filter) {
return deleteFilesInDirWithFilter(getFileByPath(dirPath), filter);
}
public static boolean deleteFilesInDirWithFilter(final File dir, final FileFilter filter) {
if (dir == null) return false;
// dir doesn't exist then return true
if (!dir.exists()) return true;
// dir isn't a directory then return false
if (!dir.isDirectory()) return false;
File[] files = dir.listFiles();
if (files != null && files.length != 0) {
for (File file : files) {
if (filter.accept(file)) {
if (file.isFile()) {
if (!file.delete()) return false;
} else if (file.isDirectory()) {
if (!deleteDir(file)) return false;
}
}
}
}
return true;
}
public interface OnReplaceListener {
boolean onReplace();
}
/*------------------------------------- inner methods ---------------------------------------*/
private static boolean createOrExistsDir(final File file) {
return file != null && (file.exists() ? file.isDirectory() : file.mkdirs());
}
private static boolean copyOrMoveFile(final File srcFile, final File destFile, final boolean isMove) {
return copyOrMoveFile(srcFile, destFile, new OnReplaceListener() {
@Override
public boolean onReplace() {
return true;
}
}, isMove);
}
private static boolean copyOrMoveFile(final File srcFile, final File destFile, final OnReplaceListener listener, final boolean isMove) {
if (srcFile == null || destFile == null) return false;
// srcFile equals destFile then return false
if (srcFile.equals(destFile)) return false;
// srcFile doesn't exist or isn't a file then return false
if (!srcFile.exists() || !srcFile.isFile()) return false;
if (destFile.exists()) {
if (listener == null || listener.onReplace()) {// require delete the old file
if (!destFile.delete()) {// unsuccessfully delete then return false
return false;
}
} else {
return true;
}
}
if (!createOrExistsDir(destFile.getParentFile())) return false;
try {
return writeFileFromIS(destFile, new FileInputStream(srcFile)) && !(isMove && !deleteFile(srcFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
}
private static boolean copyOrMoveDir(final File srcDir, final File destDir, final boolean isMove) {
return copyOrMoveDir(srcDir, destDir, new OnReplaceListener() {
@Override
public boolean onReplace() {
return true;
}
}, isMove);
}
private static boolean copyOrMoveDir(final File srcDir, final File destDir, final OnReplaceListener listener, final boolean isMove) {
if (srcDir == null || destDir == null) return false;
// destDir's path locate in srcDir's path then return false
String srcPath = srcDir.getPath() + File.separator;
String destPath = destDir.getPath() + File.separator;
if (destPath.contains(srcPath)) return false;
if (!srcDir.exists() || !srcDir.isDirectory()) return false;
if (destDir.exists()) {
if (listener == null || listener.onReplace()) {// require delete the old directory
if (!deleteAllInDir(destDir)) {// unsuccessfully delete then return false
return false;
}
} else {
return true;
}
}
if (!createOrExistsDir(destDir)) return false;
File[] files = srcDir.listFiles();
for (File file : files) {
File oneDestFile = new File(destPath + file.getName());
if (file.isFile()) {
if (!copyOrMoveFile(file, oneDestFile, listener, isMove)) return false;
} else if (file.isDirectory()) {
if (!copyOrMoveDir(file, oneDestFile, listener, isMove)) return false;
}
}
return !isMove || deleteDir(srcDir);
}
private static boolean writeFileFromIS(final File file, final InputStream is) {
OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(file));
byte data[] = new byte[8192];
int len;
while ((len = is.read(data, 0, 8192)) != -1) {
os.write(data, 0, len);
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
private static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
private static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
@Nullable
private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {column};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
return null;
}
private static Uri createURI(String type){
switch (type) {
case "image":
return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
case "video":
return MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
case "audio":
return MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
default:
break;
}
return null;
}
private static String byte2FitMemorySize(final long byteNum) {
if (byteNum < 0) {
return "shouldn't be less than zero!";
} else if (byteNum < 1024) {
return String.format(Locale.getDefault(), "%.3fB", (double) byteNum);
} else if (byteNum < 1048576) {
return String.format(Locale.getDefault(), "%.3fKB", (double) byteNum / 1024);
} else if (byteNum < 1073741824) {
return String.format(Locale.getDefault(), "%.3fMB", (double) byteNum / 1048576);
} else {
return String.format(Locale.getDefault(), "%.3fGB", (double) byteNum / 1073741824);
}
}
private FileUtils() {
throw new UnsupportedOperationException("u can't initialize me!");
}
}