|
@@ -0,0 +1,397 @@
|
|
1
|
+package com.xhly.corelib.utils;
|
|
2
|
+
|
|
3
|
+import android.content.Context;
|
|
4
|
+import android.os.Handler;
|
|
5
|
+import android.os.Looper;
|
|
6
|
+import android.os.Message;
|
|
7
|
+import android.util.Log;
|
|
8
|
+
|
|
9
|
+import java.io.BufferedReader;
|
|
10
|
+import java.io.ByteArrayOutputStream;
|
|
11
|
+import java.io.File;
|
|
12
|
+import java.io.FileInputStream;
|
|
13
|
+import java.io.FileOutputStream;
|
|
14
|
+import java.io.IOException;
|
|
15
|
+import java.io.InputStream;
|
|
16
|
+import java.io.InputStreamReader;
|
|
17
|
+import java.io.OutputStream;
|
|
18
|
+import java.util.Arrays;
|
|
19
|
+import java.util.Comparator;
|
|
20
|
+
|
|
21
|
+/**
|
|
22
|
+ * Created by YQ015 on 2018/4/12.
|
|
23
|
+ */
|
|
24
|
+
|
|
25
|
+public class FileUtils {
|
|
26
|
+
|
|
27
|
+ private static FileUtils instance;
|
|
28
|
+ private static final int SUCCESS = 1;
|
|
29
|
+ private static final int FAILED = 0;
|
|
30
|
+ private Context context;
|
|
31
|
+ private FileOperateCallback callback;
|
|
32
|
+ private volatile boolean isSuccess;
|
|
33
|
+ private String errorStr;
|
|
34
|
+
|
|
35
|
+ public static FileUtils getInstance(Context context) {
|
|
36
|
+ if (instance == null)
|
|
37
|
+ instance = new FileUtils(context);
|
|
38
|
+ return instance;
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ public static FileUtils getInstance() {
|
|
42
|
+ if (instance == null)
|
|
43
|
+ instance = new FileUtils();
|
|
44
|
+ return instance;
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ private FileUtils() {
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ private FileUtils(Context context) {
|
|
51
|
+ this.context = context;
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ private Handler handler = new Handler(Looper.getMainLooper()) {
|
|
55
|
+ @Override
|
|
56
|
+ public void handleMessage(Message msg) {
|
|
57
|
+ super.handleMessage(msg);
|
|
58
|
+ if (callback != null) {
|
|
59
|
+ if (msg.what == SUCCESS) {
|
|
60
|
+ callback.onSuccess();
|
|
61
|
+ }
|
|
62
|
+ if (msg.what == FAILED) {
|
|
63
|
+ callback.onFailed(msg.obj.toString());
|
|
64
|
+ }
|
|
65
|
+ }
|
|
66
|
+ }
|
|
67
|
+ };
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+ public static void moveFile(String fromFile, String toFile) {
|
|
71
|
+ //安卓9.0多出两个缓存文件wal和shm
|
|
72
|
+ File fromfile = new File(fromFile);
|
|
73
|
+ File tofile = new File(toFile);
|
|
74
|
+ if (!tofile.exists()) {
|
|
75
|
+ try {
|
|
76
|
+ if (tofile.isDirectory()) {
|
|
77
|
+ tofile.mkdir();
|
|
78
|
+ } else {
|
|
79
|
+ tofile.createNewFile();
|
|
80
|
+ }
|
|
81
|
+ } catch (IOException e) {
|
|
82
|
+ e.printStackTrace();
|
|
83
|
+ }
|
|
84
|
+ }
|
|
85
|
+ CopyFile(fromfile.toString(), tofile.toString());
|
|
86
|
+ if (fromfile.exists()) {
|
|
87
|
+ fromfile.delete();
|
|
88
|
+ }
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ public FileUtils copyAssetsToSD(final String srcPath, final String sdPath) {
|
|
92
|
+ new Thread(new Runnable() {
|
|
93
|
+ @Override
|
|
94
|
+ public void run() {
|
|
95
|
+ copyAssetsToDst(context, srcPath, sdPath);
|
|
96
|
+ Log.d(this.getClass().getName(),"失败没有="+isSuccess);
|
|
97
|
+ if (isSuccess)
|
|
98
|
+ handler.obtainMessage(SUCCESS).sendToTarget();
|
|
99
|
+ else
|
|
100
|
+ handler.obtainMessage(FAILED, errorStr).sendToTarget();
|
|
101
|
+ }
|
|
102
|
+ }).start();
|
|
103
|
+ return this;
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ public static void CopyDir(String sourcePath, String newPath) throws IOException {
|
|
107
|
+ File file = new File(sourcePath);
|
|
108
|
+ String[] filePath = file.list();
|
|
109
|
+
|
|
110
|
+ if (!(new File(newPath)).exists()) {
|
|
111
|
+ (new File(newPath)).mkdir();
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ for (int i = 0; i < filePath.length; i++) {
|
|
115
|
+ if ((new File(sourcePath + file.separator + filePath[i])).isDirectory()) {
|
|
116
|
+ CopyDir(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ if (new File(sourcePath + file.separator + filePath[i]).isFile()) {
|
|
120
|
+ CopyFile(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
|
|
121
|
+ }
|
|
122
|
+
|
|
123
|
+ }
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+ public static int CopyFile(String fromFile, String toFile) {
|
|
127
|
+ try {
|
|
128
|
+ InputStream fosfrom = new FileInputStream(fromFile);
|
|
129
|
+ OutputStream fosto = new FileOutputStream(toFile);
|
|
130
|
+ byte bt[] = new byte[1024];
|
|
131
|
+ int c;
|
|
132
|
+ while ((c = fosfrom.read(bt)) > 0) {
|
|
133
|
+ fosto.write(bt, 0, c);
|
|
134
|
+ }
|
|
135
|
+ fosfrom.close();
|
|
136
|
+ fosto.close();
|
|
137
|
+ return 0;
|
|
138
|
+
|
|
139
|
+ } catch (Exception ex) {
|
|
140
|
+ return -1;
|
|
141
|
+ }
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ public static int saveInputStreamFile(InputStream fosfrom, String toFile) {
|
|
145
|
+ try {
|
|
146
|
+ OutputStream fosto = new FileOutputStream(toFile);
|
|
147
|
+ byte bt[] = new byte[1024];
|
|
148
|
+ int c;
|
|
149
|
+ while ((c = fosfrom.read(bt)) > 0) {
|
|
150
|
+ fosto.write(bt, 0, c);
|
|
151
|
+ }
|
|
152
|
+ fosfrom.close();
|
|
153
|
+ fosto.close();
|
|
154
|
+ return 0;
|
|
155
|
+
|
|
156
|
+ } catch (Exception ex) {
|
|
157
|
+ return -1;
|
|
158
|
+ }
|
|
159
|
+ }
|
|
160
|
+
|
|
161
|
+ /**
|
|
162
|
+ * 删除目录下的文件,不涉及下一级目录,不删除目录本身
|
|
163
|
+ * @param file 目录
|
|
164
|
+ * @return 0成功
|
|
165
|
+ */
|
|
166
|
+ public static int deleteCurrentFlie(File file) {
|
|
167
|
+ try {
|
|
168
|
+ if (file.isDirectory()) {
|
|
169
|
+ File[] files = file.listFiles();
|
|
170
|
+ for (int i = 0; i < files.length; i++) {
|
|
171
|
+ files[i].delete();
|
|
172
|
+ }
|
|
173
|
+ }
|
|
174
|
+ return 0;
|
|
175
|
+ } catch (Exception ex) {
|
|
176
|
+ return -1;
|
|
177
|
+ }
|
|
178
|
+ }
|
|
179
|
+
|
|
180
|
+ //删除目录下的文件,只保留最新1条,不涉及下一级目录,不删除目录本身
|
|
181
|
+ public static int deleteOnlyOneFlie(File file) {
|
|
182
|
+ try {
|
|
183
|
+ if (file.isDirectory()) {
|
|
184
|
+ File[] files = file.listFiles();
|
|
185
|
+ // 根据文件的最后修改时间进行排序
|
|
186
|
+ Arrays.sort(files, Comparator.comparingLong(File::lastModified).reversed());
|
|
187
|
+ // 保留最新文件的数量
|
|
188
|
+ int keepCount = 1;
|
|
189
|
+ for (int i = keepCount; i < files.length; i++) {
|
|
190
|
+ files[i].delete();
|
|
191
|
+ }
|
|
192
|
+ }
|
|
193
|
+ return 0;
|
|
194
|
+ } catch (Exception ex) {
|
|
195
|
+ return -1;
|
|
196
|
+ }
|
|
197
|
+ }
|
|
198
|
+
|
|
199
|
+ public static boolean deleteFile(String fileName) {
|
|
200
|
+ File file = new File(fileName);
|
|
201
|
+ // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
|
|
202
|
+ if (file.exists() && file.isFile()) {
|
|
203
|
+ if (file.delete()) {
|
|
204
|
+ return true;
|
|
205
|
+ } else {
|
|
206
|
+ return false;
|
|
207
|
+ }
|
|
208
|
+ } else {
|
|
209
|
+ return false;
|
|
210
|
+ }
|
|
211
|
+ }
|
|
212
|
+
|
|
213
|
+ /**
|
|
214
|
+ * 删除目录及目录下的文件
|
|
215
|
+ *
|
|
216
|
+ * @param dir 要删除的目录的文件路径
|
|
217
|
+ * @return 目录删除成功返回true,否则返回false
|
|
218
|
+ */
|
|
219
|
+ public static boolean deleteDirectory(String dir) {
|
|
220
|
+ // 如果dir不以文件分隔符结尾,自动添加文件分隔符
|
|
221
|
+ if (!dir.endsWith(File.separator))
|
|
222
|
+ dir = dir + File.separator;
|
|
223
|
+ File dirFile = new File(dir);
|
|
224
|
+ // 如果dir对应的文件不存在,或者不是一个目录,则退出
|
|
225
|
+ if ((!dirFile.exists()) || (!dirFile.isDirectory())) {
|
|
226
|
+ return false;
|
|
227
|
+ }
|
|
228
|
+ boolean flag = true;
|
|
229
|
+ // 删除文件夹中的所有文件包括子目录
|
|
230
|
+ File[] files = dirFile.listFiles();
|
|
231
|
+ for (int i = 0; i < files.length; i++) {
|
|
232
|
+ // 删除子文件
|
|
233
|
+ if (files[i].isFile()) {
|
|
234
|
+ flag = FileUtils.deleteFile(files[i].getAbsolutePath());
|
|
235
|
+ if (!flag)
|
|
236
|
+ break;
|
|
237
|
+ }
|
|
238
|
+ // 删除子目录
|
|
239
|
+ else if (files[i].isDirectory()) {
|
|
240
|
+ flag = FileUtils.deleteDirectory(files[i]
|
|
241
|
+ .getAbsolutePath());
|
|
242
|
+ if (!flag)
|
|
243
|
+ break;
|
|
244
|
+ }
|
|
245
|
+ }
|
|
246
|
+ if (!flag) {
|
|
247
|
+ return false;
|
|
248
|
+ }
|
|
249
|
+ // 删除当前目录
|
|
250
|
+ if (dirFile.toString().equals(dir.toString())) {
|
|
251
|
+ return true;
|
|
252
|
+ } else {
|
|
253
|
+ if (dirFile.delete()) {
|
|
254
|
+ return true;
|
|
255
|
+ } else {
|
|
256
|
+ return false;
|
|
257
|
+ }
|
|
258
|
+ }
|
|
259
|
+ }
|
|
260
|
+
|
|
261
|
+ public void setFileOperateCallback(FileOperateCallback callback) {
|
|
262
|
+ this.callback = callback;
|
|
263
|
+ }
|
|
264
|
+
|
|
265
|
+ private void copyAssetsToDst(Context context, String srcPath, String dstPath) {
|
|
266
|
+ try {
|
|
267
|
+ File outFile = new File(dstPath);
|
|
268
|
+ if (!outFile.exists()) {
|
|
269
|
+ outFile.createNewFile();
|
|
270
|
+ InputStream is = context.getAssets().open(srcPath);
|
|
271
|
+ FileOutputStream fos = new FileOutputStream(outFile);
|
|
272
|
+ byte[] buffer = new byte[1024];
|
|
273
|
+ int byteCount;
|
|
274
|
+ while ((byteCount = is.read(buffer)) != -1) {
|
|
275
|
+ fos.write(buffer, 0, byteCount);
|
|
276
|
+ }
|
|
277
|
+ fos.flush();
|
|
278
|
+ is.close();
|
|
279
|
+ fos.close();
|
|
280
|
+ }
|
|
281
|
+ isSuccess = true;
|
|
282
|
+ } catch (Exception e) {
|
|
283
|
+ e.printStackTrace();
|
|
284
|
+ errorStr = e.getMessage();
|
|
285
|
+ isSuccess = false;
|
|
286
|
+ Log.d(this.getClass().getName(),"失败"+e.toString());
|
|
287
|
+ }
|
|
288
|
+ }
|
|
289
|
+
|
|
290
|
+ public byte[] getAssetsRes(Context context, String srcPath) {
|
|
291
|
+ try {
|
|
292
|
+ InputStream is = context.getAssets().open(srcPath);
|
|
293
|
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
294
|
+ byte[] buffer = new byte[4096];
|
|
295
|
+ int n = 0;
|
|
296
|
+ while (-1 != (n = is.read(buffer))) {
|
|
297
|
+ output.write(buffer, 0, n);
|
|
298
|
+ }
|
|
299
|
+ byte[] bytes = output.toByteArray();
|
|
300
|
+ return bytes;
|
|
301
|
+ } catch (IOException e) {
|
|
302
|
+ e.printStackTrace();
|
|
303
|
+ }
|
|
304
|
+ return null;
|
|
305
|
+ }
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+ public String getAssetsResource(Context context, String srcPath) {
|
|
309
|
+ try {
|
|
310
|
+ InputStream is = context.getAssets().open(srcPath);
|
|
311
|
+ InputStreamReader inputStreamReader = null;
|
|
312
|
+ inputStreamReader = new InputStreamReader(is, "UTF-8");
|
|
313
|
+ BufferedReader reader = new BufferedReader(inputStreamReader);
|
|
314
|
+ StringBuffer sb = new StringBuffer("");
|
|
315
|
+ String line;
|
|
316
|
+ while ((line = reader.readLine()) != null) {
|
|
317
|
+ //添加到字符缓冲流中
|
|
318
|
+ sb.append(line);
|
|
319
|
+ sb.append("\n");
|
|
320
|
+ }
|
|
321
|
+ return sb.toString();
|
|
322
|
+ } catch (IOException e) {
|
|
323
|
+ e.printStackTrace();
|
|
324
|
+ }
|
|
325
|
+ return null;
|
|
326
|
+ }
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+ public void writeFileData(String filename, String content, Context context) {
|
|
330
|
+ //写入文件到File目录下
|
|
331
|
+ try {
|
|
332
|
+ FileOutputStream fos = context.openFileOutput(filename, context.MODE_PRIVATE);//获得FileOutputStream
|
|
333
|
+ //将要写入的字符串转换为byte数组
|
|
334
|
+ byte[] bytes = content.getBytes();
|
|
335
|
+ fos.write(bytes);//将byte数组写入文件
|
|
336
|
+ fos.close();//关闭文件输出流
|
|
337
|
+
|
|
338
|
+ } catch (Exception e) {
|
|
339
|
+ e.printStackTrace();
|
|
340
|
+ }
|
|
341
|
+ }
|
|
342
|
+
|
|
343
|
+ public void writeFileToPath(String filename, String content) {
|
|
344
|
+ //写入文件到自定义目录下
|
|
345
|
+ try {
|
|
346
|
+ FileOutputStream fos = new FileOutputStream(filename);
|
|
347
|
+ //将要写入的字符串转换为byte数组
|
|
348
|
+ byte[] bytes = content.getBytes();
|
|
349
|
+ fos.write(bytes);//将byte数组写入文件
|
|
350
|
+ fos.close();//关闭文件输出流
|
|
351
|
+
|
|
352
|
+ } catch (Exception e) {
|
|
353
|
+ e.printStackTrace();
|
|
354
|
+ }
|
|
355
|
+ }
|
|
356
|
+
|
|
357
|
+ public void writeFileToPath(String filename, byte[] bytes) {
|
|
358
|
+ //写入文件到自定义目录下
|
|
359
|
+ try {
|
|
360
|
+ FileOutputStream fos = new FileOutputStream(filename);
|
|
361
|
+ //将要写入的字符串转换为byte数组
|
|
362
|
+ fos.write(bytes);//将byte数组写入文件
|
|
363
|
+ fos.close();//关闭文件输出流
|
|
364
|
+
|
|
365
|
+ } catch (Exception e) {
|
|
366
|
+ e.printStackTrace();
|
|
367
|
+ }
|
|
368
|
+ }
|
|
369
|
+
|
|
370
|
+ public String readFileToPath(String filename) {
|
|
371
|
+ //读取自定义目录下文件
|
|
372
|
+ try {
|
|
373
|
+ FileInputStream is = new FileInputStream(filename);
|
|
374
|
+ InputStreamReader inputStreamReader = null;
|
|
375
|
+ inputStreamReader = new InputStreamReader(is, "UTF-8");
|
|
376
|
+ BufferedReader reader = new BufferedReader(inputStreamReader);
|
|
377
|
+ StringBuffer sb = new StringBuffer("");
|
|
378
|
+ String line;
|
|
379
|
+ while ((line = reader.readLine()) != null) {
|
|
380
|
+ //添加到字符缓冲流中
|
|
381
|
+ sb.append(line);
|
|
382
|
+ sb.append("\n");
|
|
383
|
+ }
|
|
384
|
+ return sb.toString();
|
|
385
|
+ } catch (IOException e) {
|
|
386
|
+ e.printStackTrace();
|
|
387
|
+ }
|
|
388
|
+ return null;
|
|
389
|
+ }
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+ public interface FileOperateCallback {
|
|
393
|
+ void onSuccess();
|
|
394
|
+
|
|
395
|
+ void onFailed(String error);
|
|
396
|
+ }
|
|
397
|
+}
|