hxb
2023-06-28 60f74b306659cba1a8ed7378f9df54a71e48a614
app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java
@@ -2,9 +2,11 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Log;
import com.hdl.photovoltaic.HDLApp;
import com.hdl.photovoltaic.config.UserConfigManage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
@@ -13,10 +15,18 @@
import java.io.FileOutputStream;
import java.io.IOException;
/**
 * 文件操作的逻辑
 */
public class HdlFileLogic {
    private static volatile HdlFileLogic sHdlFileLogic;
    /**
     * 获取当前对象
     *
     * @return HdlFileLogic
     */
    public static synchronized HdlFileLogic getInstance() {
        if (sHdlFileLogic == null) {
            synchronized (HdlFileLogic.class) {
@@ -31,84 +41,99 @@
    private static final String TAG = "FileUtils";
    public static final String userId = "";
    private String getUserId() {
        return UserConfigManage.getInstance().getUserId();
    }
    private String getHomeId() {
        return UserConfigManage.getInstance().getHomeId();
    }
    /**
     * 获取内部存储文件路径
     * 获取手机内部存储文件路径
     */
    private String getInternalStoreFilesPath() {
    private String getAPPInternalStoreFilesPath() {
        return HDLApp.getInstance().getFilesDir().getAbsolutePath();
    }
    /**
     * 获取App存放文件的根路径
     * 获取存放文件【用户】根路径
     */
    public String getAppFilesPath() {
        return HDLApp.getInstance().getFilesDir().getAbsolutePath() + userId + "/home";
    public String getUserFilesPath() {
        return HDLApp.getInstance().getFilesDir().getAbsolutePath() + getUserId();
    }
    public File createFile(String dirPath, String fileName) {
    /**
     * 获取存放文件【住宅】根路径
     */
    public String getHomeFilesPath() {
        return HDLApp.getInstance().getFilesDir().getAbsolutePath() + getUserId() + "/home";
    }
    /**
     * 住宅名称
     *
     * @return -
     */
    public String getHomeFileName() {
        return getHomeId() + ".json";
    }
    /**
     * 创建文件
     *
     * @param fullPath 全路径
     * @return -
     */
    public boolean createFile(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(dirPath, fileName);
            File file = new File(fullPath);
            if (!file.exists()) {
                if (!file.createNewFile()) {
                    Log.e(TAG, "createFile createNewFile fail");
                    return null;
                }
                boolean succeed = file.createNewFile();
                HdlLogLogic.print("创建文件==" + succeed);
                return succeed;
            }
            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 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 createFileDir(String fullPath) {
        try {
            File file = new File(fullPath);
            if (!file.isDirectory()) {
                boolean succeed = file.mkdirs();
                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;
                }
            }
@@ -122,6 +147,12 @@
    }
    /**
     * 读取文件
     *
     * @param filepath 全路径
     * @return 数据
     */
    public String readFile(String filepath) {
        try {
            String fileContent = "";
@@ -179,10 +210,62 @@
            //关闭缓存输入流
            bis.close();
            return bitmap;
        } catch (Exception e) {
        }
        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;
        }
    }
}