From 4e0c05778454d424835330eb6f5c88fca20ac6af Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期二, 27 六月 2023 20:01:43 +0800
Subject: [PATCH] 2023年06月27日20:01:34

---
 app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java |  150 ++++++++++++++++++++++++++++++++------------------
 1 files changed, 96 insertions(+), 54 deletions(-)

diff --git a/app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java b/app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java
index ee5c470..368ad9f 100644
--- a/app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java
+++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java
@@ -2,6 +2,7 @@
 
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
+import android.text.TextUtils;
 import android.util.Log;
 
 import com.hdl.photovoltaic.HDLApp;
@@ -15,7 +16,7 @@
 import java.io.IOException;
 
 /**
- * 鏂囦欢鐨勯�昏緫
+ * 鏂囦欢鎿嶄綔鐨勯�昏緫
  */
 public class HdlFileLogic {
 
@@ -56,14 +57,14 @@
     }
 
     /**
-     * 鑾峰彇瀛樻斁鏂囦欢鐢ㄦ埛鏍硅矾寰�
+     * 鑾峰彇瀛樻斁鏂囦欢銆愮敤鎴枫�戞牴璺緞
      */
     public String getUserFilesPath() {
         return HDLApp.getInstance().getFilesDir().getAbsolutePath() + getUserId();
     }
 
     /**
-     * 鑾峰彇瀛樻斁鏂囦欢浣忓畢鏍硅矾寰�
+     * 鑾峰彇瀛樻斁鏂囦欢銆愪綇瀹呫�戞牴璺緞
      */
     public String getHomeFilesPath() {
         return HDLApp.getInstance().getFilesDir().getAbsolutePath() + getUserId() + "/home";
@@ -78,68 +79,61 @@
         return getHomeId() + ".json";
     }
 
-    public File createFile(String dirPath, String fileName) {
+    /**
+     * 鍒涘缓鏂囦欢
+     *
+     * @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;
                 }
             }
@@ -153,6 +147,12 @@
 
     }
 
+    /**
+     * 璇诲彇鏂囦欢
+     *
+     * @param filepath 鍏ㄨ矾寰�
+     * @return 鏁版嵁
+     */
     public String readFile(String filepath) {
         try {
             String fileContent = "";
@@ -210,8 +210,6 @@
             //鍏抽棴缂撳瓨杈撳叆娴�
             bis.close();
             return bitmap;
-
-
         } catch (Exception e) {
         }
         return null;
@@ -222,8 +220,52 @@
      */
     public void createDirectory() {
         //鐢ㄦ埛淇℃伅
-        this.createFileDir(new File(this.getUserFilesPath()));
+        this.createFileDir(this.getUserFilesPath());
         //浣忓畢淇℃伅
-        this.createFileDir(new File(this.getHomeFilesPath()));
+        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;
+        }
+
     }
 }

--
Gitblit v1.8.0