wjc
2023-06-28 975b91521a04e159f45fb34fc7b55afbf455f7f5
app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java
@@ -2,6 +2,7 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import com.hdl.photovoltaic.HDLApp;
@@ -12,10 +13,9 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 * 文件的逻辑
 * 文件操作的逻辑
 */
public class HdlFileLogic {
@@ -47,99 +47,158 @@
    private String getHomeId() {
        return UserConfigManage.getInstance().getHomeId();
    }
    //region    ---------root路径-----------
    /**
     * 获取手机内部存储文件路径
     */
    private String getAPPInternalStoreFilesPath() {
        return HDLApp.getInstance().getFilesDir().getAbsolutePath();
        return HDLApp.getInstance().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).getPath();
    }
    /**
     * 获取存放文件用户根路径
     * 获取存放文件【用户】根路径
     */
    public String getUserFilesPath() {
        return HDLApp.getInstance().getFilesDir().getAbsolutePath() + getUserId();
    public String getCurrentUserRootPath() {
        return getAPPInternalStoreFilesPath() + "/" + getUserId();
    }
    /**
     * 获取存放文件住宅根路径
     * 获取存放文件【住宅】根路径
     */
    public String getHomeFilesPath() {
        return HDLApp.getInstance().getFilesDir().getAbsolutePath() + getUserId() + "/home";
    public String getCurrentHomeRootPath() {
        return getAPPInternalStoreFilesPath() + "/" + getUserId() + "/home";
    }
    /**
     * 住宅名称
     * 住宅文件名称
     *
     * @return -
     */
    public String getHomeFileName() {
        return getHomeId() + ".json";
    public String getCurrentHomeFileName() {
        return "/" + getHomeId() + ".json";
    }
    public File createFile(String dirPath, String fileName) {
    /**
     * 获取当前住宅文件全路径
     */
    public String getCurrentHomeFilesFullPath() {
        return getCurrentHomeRootPath() + getCurrentHomeFileName();
    }
    //endregion
    //region    ---------【文件夹】操作-----------
    /**
     * 预创建文件夹
     */
    public void createDirectory() {
        //存放用户信息
        this.createFileDir(this.getCurrentUserRootPath());
        //存放住宅信息
        this.createFileDir(this.getCurrentHomeRootPath());
    }
    /**
     * 创建文件夹
     *
     * @param fullPath fullPath 全路径
     * @return -
     */
    public boolean createFileDir(String fullPath) {
        try {
            File dirFile = new File(dirPath);
            if (!dirFile.exists()) {
                if (!createFileDir(dirFile)) {
                    HdlLogLogic.print(TAG, "createFile dirFile.mkdirs fail");
                    return null;
                }
            } else if (!dirFile.isDirectory()) {
                boolean delete = dirFile.delete();
                if (delete) {
                    return createFile(dirPath, fileName);
                } else {
                    HdlLogLogic.print(TAG, "createFile dirFile !isDirectory and delete fail");
                    return null;
                }
            File file = new File(fullPath);
            if (!file.isDirectory()) {
                boolean succeed = file.mkdirs();
                HdlLogLogic.print("创建文件夹==" + succeed);
                return succeed;
            }
            File file = new File(dirPath, fileName);
            if (!file.exists()) {
                if (!file.createNewFile()) {
                    Log.e(TAG, "createFile createNewFile fail");
                    return null;
                }
            }
            return file;
            return true;
        } catch (Exception e) {
            HdlLogLogic.print(TAG, "createFile fail :" + e.getMessage());
            e.printStackTrace();
            return null;
            HdlLogLogic.print("创建文件夹有异常==" + e.getMessage());
            return false;
        }
    }
    /**
     * 删除文件夹
     *
     * @param fullPath 全路径
     */
    public void deleteDirectory(String fullPath) {
        try {
            File file = new File(fullPath);
            if (file.isDirectory()) {
                boolean succeed = file.delete();
                HdlLogLogic.print("删除文件夹==" + succeed);
            }
        } catch (Exception e) {
            HdlLogLogic.print("删除文件夹有异常==" + 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();
                HdlLogLogic.print("创建文件==" + succeed);
                return succeed;
            }
            return true;
        } catch (Exception e) {
            HdlLogLogic.print("创建文件有异常==" + e.getMessage());
            return false;
        }
    }
    /**
     * 创建文件夹---之所以要一层层创建,是因为一次性创建多层文件夹可能会失败!
     * 删除文件
     *
     * @param fullPath 全路径
     * @return -
     */
    public boolean createFileDir(File dirFile) {
        if (dirFile == null) {
            return true;
        }
        if (dirFile.exists()) {
            return true;
        }
        File parentFile = dirFile.getParentFile();
        if (parentFile != null && !parentFile.exists()) {
            //父文件夹不存在,则先创建父文件夹,再创建自身文件夹
            return createFileDir(parentFile) && createFileDir(dirFile);
        } else {
            boolean mkdirs = dirFile.mkdirs();
            boolean isSuccess = mkdirs || dirFile.exists();
            if (!isSuccess) {
                Log.e("FileUtil", "createFileDir fail " + dirFile);
    public boolean deleteFile(String fullPath) {
        try {
            File file = new File(fullPath);
            if (file.exists()) {
                boolean succeed = file.delete();
                HdlLogLogic.print("删除文件==" + succeed);
                return succeed;
            }
            return isSuccess;
            return true;
        } catch (Exception e) {
            HdlLogLogic.print("删除文件有异常==" + e.getMessage());
            return false;
        }
    }
    public void writeFile(String path, String data) {
    /**
     * 写入文件
     *
     * @param fullPath 全路径
     * @param data     数据
     */
    public void writeFile(String fullPath, String data) {
        try {
            File file = new File(path);
            File file = new File(fullPath);
            if (!file.exists()) {
                if (!file.mkdir() && !file.isDirectory()) {
                    HdlLogLogic.print(TAG, "Error: make dir failed!");
                if (!createFile(fullPath)) {
                    //创建失败直接返回
                    return;
                }
            }
@@ -147,12 +206,19 @@
            d.write(data.getBytes());
            d.flush();
            d.close();
        } catch (IOException e) {
            e.printStackTrace();
            HdlLogLogic.print("写入文件成功==" + fullPath);
        } catch (Exception e) {
            HdlLogLogic.print("写入文件有异常==" + e.getMessage());
        }
    }
    /**
     * 读取文件
     *
     * @param filepath 全路径
     * @return 数据
     */
    public String readFile(String filepath) {
        try {
            String fileContent = "";
@@ -169,15 +235,16 @@
            byte[] bytes = new byte[fis.available()];
            fis.read(bytes);
            fis.close();
            HdlLogLogic.print("读取文件成功==" + filepath);
            fileContent = new String(bytes);
            return fileContent;
        } catch (Exception e1) {
            e1.printStackTrace();
            Log.d(TAG, "Error: Input File not find!");
            HdlLogLogic.print("读取文件有异常==" + e1.getMessage());
            return "";
        }
    }
    //endregion
    /**
     * 把位图数据保存到指定路径的图片文件
@@ -197,6 +264,7 @@
        } catch (Exception e) {
        }
    }
    //</editor-fold>
    /**
     * 从指定路径的图片文件中读取位图数据
@@ -210,20 +278,10 @@
            //关闭缓存输入流
            bis.close();
            return bitmap;
        } catch (Exception e) {
        }
        return null;
    }
    /**
     * 预创建文件夹
     */
    public void createDirectory() {
        //用户信息
        this.createFileDir(new File(this.getUserFilesPath()));
        //住宅信息
        this.createFileDir(new File(this.getHomeFilesPath()));
    }
}