mac
2023-11-20 734babb3a7348050fdffe845c560ba8b0b218152
app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java
@@ -3,16 +3,19 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import com.hdl.photovoltaic.HDLApp;
import com.hdl.photovoltaic.config.UserConfigManage;
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.InputStream;
import java.io.InputStreamReader;
import java.util.Objects;
/**
 * 文件操作的逻辑
@@ -20,6 +23,11 @@
public class HdlFileLogic {
    private static volatile HdlFileLogic sHdlFileLogic;
    /**
     * 表示1m大小数据
     */
    private final int m = 1;
    /**
     * 获取当前对象
@@ -52,39 +60,51 @@
    /**
     * 获取手机内部存储文件路径
     */
    private String getAPPInternalStoreFilesPath() {
        return HDLApp.getInstance().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).getPath();
    public String getAPPInternalStoreFilesPath() {
        return Objects.requireNonNull(HDLApp.getInstance().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)).getPath();
    }
    /**
     * 获取存放文件【用户】根路径
     * 获取存放【用户文件夹】根路径
     */
    public String getCurrentUserRootPath() {
        return getAPPInternalStoreFilesPath() + "/" + getUserId();
    }
    /**
     * 获取存放文件【住宅】根路径
     * 获取存放【住宅文件夹】根路径
     */
    public String getCurrentHomeRootPath() {
        return getAPPInternalStoreFilesPath() + "/" + getUserId() + "/home";
        return getCurrentUserRootPath() + "/" + "home_" + getHomeId();
    }
    /**
     * 住宅文件名称
     *
     * @return -
     * 获取存放【驱动文件夹】根路径
     */
    public String getCurrentHomeFileName() {
        return "/" + getHomeId() + ".json";
    public String getDriveRootPath() {
        return getAPPInternalStoreFilesPath() + "/upgrade/drive";
    }
    /**
     * 获取当前住宅文件全路径
     * 获取存放【固件文件夹】根路径
     */
    public String getCurrentHomeFilesFullPath() {
        return getCurrentHomeRootPath() + getCurrentHomeFileName();
    public String getFirmwareRootPath() {
        return getAPPInternalStoreFilesPath() + "/upgrade/firmware";
    }
    /**
     * 获取【日志文件】全路径
     */
    public String getLogFilePath() {
        return getCurrentHomeRootPath() + "/log.txt";
    }
    /**
     * 获取【用户文件】全路径
     */
    public String getUserFilePath() {
        return getAPPInternalStoreFilesPath() + "/userConfigManage.txt";
    }
    //endregion
@@ -95,10 +115,13 @@
     * 预创建文件夹
     */
    public void createDirectory() {
        //存放用户信息
        this.createFileDir(this.getCurrentUserRootPath());
        //存放住宅信息
        this.createFileDir(this.getCurrentHomeRootPath());
        //驱动文件
        this.createFileDir(this.getDriveRootPath());
        //固件文件
        this.createFileDir(this.getFirmwareRootPath());
    }
    /**
@@ -112,12 +135,12 @@
            File file = new File(fullPath);
            if (!file.isDirectory()) {
                boolean succeed = file.mkdirs();
                HdlLogLogic.print("创建文件夹==" + succeed);
                System.out.println("创建文件夹路径=" + file.getAbsolutePath() + "->->创建文件夹结果=" + succeed);
                return succeed;
            }
            return true;
        } catch (Exception e) {
            HdlLogLogic.print("创建文件夹有异常==" + e.getMessage());
            System.out.println("创建文件夹有异常===" + e.getMessage());
            return false;
        }
    }
@@ -130,16 +153,29 @@
     */
    public void deleteDirectory(String fullPath) {
        try {
            File file = new File(fullPath);
            if (file.isDirectory()) {
                boolean succeed = file.delete();
                HdlLogLogic.print("删除文件夹==" + succeed);
            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) {
            HdlLogLogic.print("删除文件夹有异常==" + e.getMessage());
            System.out.println("删除文件夹有异常===" + e.getMessage());
        }
    }
    //endregion
    //region    ---------【文件】操作-----------
@@ -155,12 +191,12 @@
            File file = new File(fullPath);
            if (!file.exists()) {
                boolean succeed = file.createNewFile();
                HdlLogLogic.print("创建文件==" + succeed);
                System.out.println("创建文件路径=" + file.getAbsolutePath() + "->->创建文件结果=" + succeed);
                return succeed;
            }
            return true;
        } catch (Exception e) {
            HdlLogLogic.print("创建文件有异常==" + e.getMessage());
            System.out.println("创建文件有异常===" + e.getMessage());
            return false;
        }
    }
@@ -176,16 +212,17 @@
            File file = new File(fullPath);
            if (file.exists()) {
                boolean succeed = file.delete();
                HdlLogLogic.print("删除文件==" + succeed);
                System.out.println("删除文件===" + succeed);
                return succeed;
            }
            return true;
        } catch (Exception e) {
            HdlLogLogic.print("删除文件有异常==" + e.getMessage());
            System.out.println("删除文件有异常===" + e.getMessage());
            return false;
        }
    }
    /**
     * 写入文件
@@ -206,9 +243,43 @@
            d.write(data.getBytes());
            d.flush();
            d.close();
            HdlLogLogic.print("写入文件成功==" + fullPath);
            System.out.println("写入文件成功==" + fullPath);
        } catch (Exception e) {
            HdlLogLogic.print("写入文件有异常==" + e.getMessage());
            System.out.println("写入文件有异常==" + e.getMessage());
        }
    }
    /**
     * 写入文件(一行一行追加内容)
     *
     * @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 * m) {
                //文件大于1m,删除文件前10条日志
                this.delFileLien(fullPath, 10);
            }
            System.out.println("写入一行数据到文件成功===" + dataLine);
        } catch (Exception e) {
            System.out.println("写入一行数据到文件有异常===" + e.getMessage());
        }
    }
@@ -216,18 +287,16 @@
    /**
     * 读取文件
     *
     * @param filepath 全路径
     * @param filePath 全路径
     * @return 数据
     */
    public String readFile(String filepath) {
    public String readFile(String filePath) {
        try {
            String fileContent = "";
            if (null == filepath) {
                Log.d(TAG, "Error: Invalid file name!");
                return fileContent;
            if (!isBoolean(filePath)) {
                return "";
            }
            File f = new File(filepath);
            String fileContent = "";
            File f = new File(filePath);
            if (!f.exists()) {
                return fileContent;
            }
@@ -235,14 +304,68 @@
            byte[] bytes = new byte[fis.available()];
            fis.read(bytes);
            fis.close();
            HdlLogLogic.print("读取文件成功==" + filepath);
            System.out.println("读取文件成功===" + filePath);
            fileContent = new String(bytes);
            return fileContent;
        } catch (Exception e1) {
            HdlLogLogic.print("读取文件有异常==" + e1.getMessage());
            System.out.println("读取文件有异常===" + e1.getMessage());
            return "";
        }
    }
    /**
     * 指定删除文件行数(从前面删除起)
     *
     * @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 path 路径
     * @return true合法
     */
    public boolean isBoolean(String path) {
        if (path.contains("//") || path.contains("\\")) {
            System.out.println("无效文件路径===" + path);
            return false;
        }
        return true;
    }
    //endregion
@@ -261,7 +384,7 @@
            bos.close();
        } catch (Exception e) {
        } catch (Exception ignored) {
        }
    }
    //</editor-fold>