mac
2024-10-24 15c2fc33ef1023a045a76ae594b3da76a281ea20
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
package com.hdl.photovoltaic.other;
 
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.text.TextUtils;
 
import com.hdl.photovoltaic.HDLApp;
import com.hdl.photovoltaic.config.UserConfigManage;
 
import org.apache.commons.io.FileUtils;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Objects;
 
/**
 * 文件操作的逻辑
 */
public class HdlFileLogic {
 
    private static volatile HdlFileLogic sHdlFileLogic;
 
    /**
     * 表示0.5m大小数据
     */
    private final double mDataSize = 0.5;
 
    /**
     * 获取当前对象
     *
     * @return HdlFileLogic
     */
    public static synchronized HdlFileLogic getInstance() {
        if (sHdlFileLogic == null) {
            synchronized (HdlFileLogic.class) {
                if (sHdlFileLogic == null) {
                    sHdlFileLogic = new HdlFileLogic();
                }
            }
 
        }
        return sHdlFileLogic;
    }
 
    private static final String TAG = "FileUtils";
 
    private String getUserId() {
        return UserConfigManage.getInstance().getUserId();
    }
 
    private String getHomeId() {
        return UserConfigManage.getInstance().getHomeId();
    }
    //region    ---------root路径-----------
 
    /**
     * 获取手机内部存储文件路径
     */
    public String getAPPInternalStoreFilesPath() {
        return Objects.requireNonNull(HDLApp.getInstance().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)).getPath();
    }
 
    /**
     * 获取存放【用户文件夹】根路径
     */
    public String getCurrentUserRootPath() {
        return getAPPInternalStoreFilesPath() + "/" + getUserId();
    }
 
    /**
     * 获取存放【住宅文件夹】根路径
     */
    public String getCurrentHomeRootPath() {
        return getCurrentUserRootPath() + "/" + "home_" + getHomeId();
    }
 
    /**
     * 获取存放【驱动文件夹】根路径
     */
    public String getDriveRootPath() {
        return getAPPInternalStoreFilesPath() + "/upgrade/drive";
    }
 
 
    /**
     * 获取存放【固件文件夹】根路径
     */
    public String getFirmwareRootPath() {
        return getAPPInternalStoreFilesPath() + "/upgrade/firmware";
    }
 
 
 
    /**
     * 获取【日志文件】全路径
     */
    public String getLogFileNamePath() {
        return getCurrentHomeRootPath() + "/log.txt";
    }
 
    /**
     * 获取【用户文件】全路径
     */
    public String getUserFilePath() {
        return getAPPInternalStoreFilesPath() + "/userConfigManage.txt";
    }
 
    /**
     * 获取驱动升级文件全路径
     *
     * @param driverCode 驱动编码
     * @param version    驱动版本
     * @return 全路径
     */
    public String getDrivePathFileName(String driverCode, String version) {
        String fileName = driverCode + "_" + version + ".zip";
        return HdlFileLogic.getInstance().getDriveRootPath() + "/" + fileName;
    }
 
    /**
     * 获取固件升级文件全路径
     *
     * @param imageId 镜像id
     * @param version 驱动版本
     * @return 全路径
     */
    public String getFirmwarePathFileName(String imageId, String version) {
        String fileName = imageId + "_" + version + ".zip";
        return HdlFileLogic.getInstance().getFirmwareRootPath() + "/" + fileName;
    }
 
    //endregion
 
    //region    ---------【文件夹】操作-----------
 
    /**
     * 预创建文件夹
     */
    public void createDirectory() {
        //存放住宅信息
        this.createFileDir(this.getCurrentHomeRootPath());
        //驱动文件
        this.createFileDir(this.getDriveRootPath());
        //固件文件
        this.createFileDir(this.getFirmwareRootPath());
 
    }
 
    /**
     * 创建文件夹
     *
     * @param fullPath fullPath 全路径
     * @return -
     */
    public boolean createFileDir(String fullPath) {
        try {
            File file = new File(fullPath);
            if (!file.isDirectory()) {
                boolean succeed = file.mkdirs();
                System.out.println("创建文件夹路径---" + file.getAbsolutePath() + "===创建文件夹结果---" + succeed);
                return succeed;
            }
            return true;
        } catch (Exception e) {
            System.out.println("创建文件夹有异常===" + e.getMessage());
            return false;
        }
    }
 
 
    /**
     * 删除文件夹
     *
     * @param fullPath 全路径
     */
    public void deleteDirectory(String fullPath) {
        try {
            File fileRoot = new File(fullPath);
            if (fileRoot.isDirectory()) {
                File[] listFiles = fileRoot.listFiles();
                if (listFiles != null) {
                    for (File file : listFiles) {
                        if (file.isDirectory()) {
                            deleteDirectory(file.getAbsolutePath());
                        } else {
                            this.deleteFile(file.getAbsolutePath());
                        }
                    }
                }
            }
            // 删除文件夹本身
            boolean succeed = fileRoot.delete();//文件夹空这个方法才有效
            System.out.println("删除文件夹路径---" + fileRoot.getAbsolutePath() + "===删除结果---" + succeed);
        } catch (Exception e) {
            System.out.println("删除文件夹有异常===" + e.getMessage());
        }
 
    }
 
 
    //endregion
 
    //region    ---------【文件】操作-----------
 
    /**
     * 创建文件
     *
     * @param fullPath 全路径
     * @return -
     */
    public boolean createFile(String fullPath) {
        try {
            File file = new File(fullPath);
            if (!file.exists()) {
                boolean succeed = file.createNewFile();
                System.out.println("创建文件路径---" + file.getAbsolutePath() + "===创建文件结果---" + succeed);
                return succeed;
            }
            return true;
        } catch (Exception e) {
            System.out.println("创建文件有异常===" + e.getMessage());
            return false;
        }
    }
 
    /**
     * 删除文件
     *
     * @param fullPath 全路径
     * @return -
     */
    public boolean deleteFile(String fullPath) {
        try {
            File file = new File(fullPath);
            if (file.exists()) {
                boolean succeed = file.delete();
                System.out.println("删除文件---" + fullPath + "===结果---" + succeed);
                return succeed;
            }
            return true;
        } catch (Exception e) {
            System.out.println("删除文件有异常===" + e.getMessage());
            return false;
        }
 
    }
 
 
    /**
     * 写入文件
     *
     * @param fullPath 全路径
     * @param data     数据
     */
    public void writeFile(String fullPath, byte[] data) {
        try {
            File file = new File(fullPath);
            if (!file.exists()) {
                if (!createFile(fullPath)) {
                    //创建失败直接返回
                    return;
                }
            }
            FileOutputStream d = new FileOutputStream(file);
            d.write(data);
            d.flush();
            d.close();
            System.out.println("写入文件成功---" + fullPath);
        } catch (Exception e) {
            System.out.println("写入文件有异常---" + e.getMessage());
        }
 
    }
 
    /**
     * 写入文件
     *
     * @param fullPath 全路径
     * @param data     数据
     */
    public void writeFile(String fullPath, String data) {
        writeFile(fullPath, data.getBytes());
    }
 
    /**
     * 写入文件(一行一行追加内容)
     *
     * @param fullPath 全路径
     * @param dataLine 数据
     */
    public void appendFile(String fullPath, String dataLine) {
        try {
            if (!isBoolean(fullPath)) {
                return;
            }
            File file = new File(fullPath);
            if (!file.exists()) {
                if (!createFile(fullPath)) {
                    //创建失败直接返回
                    return;
                }
            }
            FileOutputStream d = new FileOutputStream(file, true);
            d.write(dataLine.getBytes());
            d.write("\r\n".getBytes());// 写入一个换行
            d.flush();
            d.close();
            if (file.length() > 1024 * 1024 * mDataSize) {
                //文件大于1m,删除文件前100条日志
                this.delFileLien(fullPath, 100);
            }
//            System.out.println("写入一行数据到文件成功---" + dataLine);
        } catch (Exception e) {
            System.out.println("写入一行数据到文件有异常---" + e.getMessage());
        }
 
    }
 
    /**
     * 读取文件
     *
     * @param filePath 全路径
     * @return 数据
     */
    public byte[] readFileByte1(String filePath) {
        try {
            if (!isBoolean(filePath)) {
                return null;
            }
            File f = new File(filePath);
            if (!f.exists()) {
                return null;
            }
            FileInputStream fis = new FileInputStream(f);
            byte[] bytes = FileUtils.readFileToByteArray(f);//这个方法不兼用android 6.0
            fis.close();
            System.out.println("读取文件成功---" + filePath);
            return bytes;
        } catch (Exception e1) {
            System.out.println("读取文件有异常---" + e1.getMessage());
            return null;
        }
 
    }
 
 
    /**
     * 读取文件
     *
     * @param filePath 全路径
     * @return 数据
     */
    public byte[] readFileByte(String filePath) {
        try {
            if (!isBoolean(filePath)) {
                return null;
            }
            File f = new File(filePath);
            if (!f.exists()) {
                return null;
            }
            FileInputStream fis = new FileInputStream(f);
            BufferedInputStream bis = new BufferedInputStream(fis);
            int fileLength = (int) f.length();
            byte[] bytes = new byte[fileLength];
            int len = bis.read(bytes);
            bis.close();
            fis.close();
            System.out.println("读取文件成功---" + filePath);
            return bytes;
        } catch (Exception e1) {
            System.out.println("读取文件有异常---" + e1.getMessage());
            return null;
        }
 
    }
 
    /**
     * 读取文件
     *
     * @param filePath 全路径
     * @return 数据
     */
    public String readFile(String filePath) {
        try {
            byte[] bytes = readFileByte(filePath);
            if (bytes == null) {
                return "";
            }
            return new String(bytes);
        } catch (Exception e1) {
            return "";
        }
 
    }
 
    /**
     * 读取文件
     *
     * @param filePath 全路径
     * @return 数据
     */
    public FileInputStream fileStream(String filePath) {
        try {
            if (!isBoolean(filePath)) {
                return null;
            }
            File f = new File(filePath);
            if (!f.exists()) {
                return null;
            }
            fileLength = f.length();
            return new FileInputStream(f);
        } catch (Exception e1) {
            System.out.println("读取文件有异常---" + e1.getMessage());
            return null;
        }
 
    }
 
    public static long fileLength = 0;
 
    /**
     * 指定删除文件行数(从前面删除起)
     *
     * @param filePath   路径
     * @param delLienSum 删除多少行数
     */
    public void delFileLien(String filePath, int delLienSum) {
        if (!isBoolean(filePath)) {
            return;
        }
        //打开文件
        File file = new File(filePath);
        //如果path是传递过来的参数,可以做一个非目录的判断
        if (file.isDirectory()) {
            System.out.println("这是一个目录");
            return;
        }
        StringBuilder content = new StringBuilder(); //文件内容字符串
        try {
            InputStream inputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            int lineCount = 0;
            //分行读取
            while ((line = bufferedReader.readLine()) != null) {
                lineCount += 1;
                if (lineCount > delLienSum) {
                    content.append(line).append("\r\n");
                }
            }
            inputStream.close();
            //删除掉旧文件
            this.deleteFile(filePath);
            //重新创建文件并且写入数据
            this.writeFile(filePath, content.toString());
        } catch (Exception ignored) {
        }
    }
 
    /**
     * 打开资源文件
     *
     * @param fileName 文件名
     * @param context  上下文
     * @return 返回json字符串
     */
    public String openAssetsFileJson(String fileName, Context context) {
        if (TextUtils.isEmpty(fileName)) {
            return "";
        }
        //将json数据变成字符串
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //获取assets资源管理器
            AssetManager assetManager = context.getAssets();
            //通过管理器打开文件并读取
            BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
 
    /**
     * 判断路径是否合法
     *
     * @param path 路径
     * @return true合法
     */
    public boolean isBoolean(String path) {
        if (path.contains("//") || path.contains("\\")) {
            System.out.println("无效文件路径---" + path);
            return false;
        }
        return true;
    }
    //endregion
 
    /**
     * 把位图数据保存到指定路径的图片文件
     */
    public void writeImage(String path, Bitmap bitmap) {
        try {
            //根据指定文件路径构建缓存输出流对象
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path));
            //把位图数据压缩到缓存输出流中
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
            //完成缓存输出流的写入动作
            bos.flush();
            //关闭缓存输出流
            bos.close();
 
 
        } catch (Exception ignored) {
        }
    }
    //</editor-fold>
 
    /**
     * 从指定路径的图片文件中读取位图数据
     */
    public Bitmap readImage(String path) {
        try {
            //根据指定文件路径构建缓存输入流对象
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
            //从缓存输入流中解码位图数据
            Bitmap bitmap = BitmapFactory.decodeStream(bis);
            //关闭缓存输入流
            bis.close();
            return bitmap;
        } catch (Exception e) {
        }
        return null;
    }
 
 
}