hxb
2023-06-28 8a6763af48a243ad3c7e04adfd6a2520d119adac
app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java
@@ -2,7 +2,7 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.os.Environment;
import android.util.Log;
import com.hdl.photovoltaic.HDLApp;
@@ -13,7 +13,6 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 * 文件操作的逻辑
@@ -48,56 +47,58 @@
    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";
    }
    /**
     * 创建文件
     *
     * @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;
        }
    public String getCurrentHomeFilesFullPath() {
        return getCurrentHomeRootPath() + getCurrentHomeFileName();
    }
    //endregion
    //region    ---------【文件夹】操作-----------
    /**
     * 预创建文件夹
     */
    public void createDirectory() {
        //存放用户信息
        this.createFileDir(this.getCurrentUserRootPath());
        //存放住宅信息
        this.createFileDir(this.getCurrentHomeRootPath());
    }
    /**
@@ -116,11 +117,75 @@
            }
            return true;
        } catch (Exception e) {
            HdlLogLogic.print("创建文件夹失败==" + e.getMessage());
            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 deleteFile(String fullPath) {
        try {
            File file = new File(fullPath);
            if (file.exists()) {
                boolean succeed = file.delete();
                HdlLogLogic.print("删除文件==" + succeed);
                return succeed;
            }
            return true;
        } catch (Exception e) {
            HdlLogLogic.print("删除文件有异常==" + e.getMessage());
            return false;
        }
    }
    /**
     * 写入文件
@@ -141,8 +206,9 @@
            d.write(data.getBytes());
            d.flush();
            d.close();
        } catch (IOException e) {
            e.printStackTrace();
            HdlLogLogic.print("写入文件成功==" + fullPath);
        } catch (Exception e) {
            HdlLogLogic.print("写入文件有异常==" + e.getMessage());
        }
    }
@@ -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>
    /**
     * 从指定路径的图片文件中读取位图数据
@@ -215,57 +283,5 @@
        return null;
    }
    /**
     * 预创建文件夹
     */
    public void createDirectory() {
        //用户信息
        this.createFileDir(this.getUserFilesPath());
        //住宅信息
        this.createFileDir(this.getHomeFilesPath());
    }
    /**
     * 删除文件
     *
     * @param fullPath 全路径
     * @return -
     */
    public boolean deleteFile(String fullPath) {
        try {
            File file = new File(fullPath);
            if (file.exists() || file.isDirectory()) {
                boolean succeed = file.delete();
                HdlLogLogic.print("删除文件==" + succeed);
                return succeed;
            }
            return true;
        } catch (Exception e) {
            HdlLogLogic.print("删除文件有异常==" + e.getMessage());
            return false;
        }
    }
    /**
     * 删除文件夹
     *
     * @param fullPath 全路径
     * @return -
     */
    public boolean deleteDirectory(String fullPath) {
        try {
            File file = new File(fullPath);
            if (file.isDirectory()) {
                boolean succeed = file.delete();
                HdlLogLogic.print("删除文件夹==" + succeed);
                return succeed;
            }
            return true;
        } catch (Exception e) {
            HdlLogLogic.print("删除文件夹有异常==" + e.getMessage());
            return false;
        }
    }
}