From ce88de4891b87c3b7b2750575e15d6e48d518852 Mon Sep 17 00:00:00 2001
From: hxb <hxb@hdlchina.com.cn>
Date: 星期四, 24 十月 2024 10:39:12 +0800
Subject: [PATCH] 增加云端库

---
 HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/utils/HDLFileUtils.java |  324 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 324 insertions(+), 0 deletions(-)

diff --git a/HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/utils/HDLFileUtils.java b/HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/utils/HDLFileUtils.java
new file mode 100644
index 0000000..09e2ed4
--- /dev/null
+++ b/HDLLinkPMSdk/src/main/java/com/hdl/linkpm/sdk/utils/HDLFileUtils.java
@@ -0,0 +1,324 @@
+package com.hdl.linkpm.sdk.utils;
+
+import android.content.Context;
+import android.content.res.AssetManager;
+
+import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+/**
+ * Created by hxb on 2022/2/24.
+ */
+public class HDLFileUtils {
+
+    /**
+     * 鍒涘缓鏂囦欢澶�
+     *
+     * @param filesPath
+     * @return
+     */
+    public static boolean createOrExistsPrivateFolder(String filesPath) {
+        File dir = new File(filesPath);
+        return createOrExistsFolder(dir);
+    }
+
+    public static String getJson(String fileName, Context context) {
+        //灏唈son鏁版嵁鍙樻垚瀛楃涓�
+        StringBuilder stringBuilder = new StringBuilder();
+        try {
+            //鑾峰彇assets璧勬簮绠$悊鍣�
+            AssetManager assetManager = context.getAssets();
+            //閫氳繃绠$悊鍣ㄦ墦寮�鏂囦欢骞惰鍙�
+            BufferedReader bf = new BufferedReader(new InputStreamReader(
+                    assetManager.open(fileName)));
+            String line;
+            while ((line = bf.readLine()) != null) {
+                stringBuilder.append(line);
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return stringBuilder.toString();
+    }
+
+    /**
+     * 鍒涘缓鏂囦欢澶�
+     *
+     * @param file
+     * @return
+     */
+    public static boolean createOrExistsFolder(File file) {
+        if (file == null)
+            return false;
+        boolean result = false;
+
+        if (isFileExists(file) && isDirectory(file)) {
+            // 濡傛灉file瀛樺湪涓旀槸鏂囦欢澶癸紝杩斿洖true
+            return true;
+        }
+        // 濡傛灉鏂囦欢澶逛笉瀛樺湪锛屽垱寤烘枃浠跺す
+        if (file.mkdirs()) {
+            // 鍒涘缓鎴愬姛杩斿洖true
+            result = true;
+        } else {
+            // 鍒涘缓澶辫触杩斿洖false
+            result = false;
+        }
+        return result;
+    }
+
+    public static boolean writeFile(String path, InputStream inputStream) {
+        FileOutputStream fop = null;
+
+        try {
+            File file = new File(path);
+            fop = new FileOutputStream(file);
+
+            if (!file.exists()) {
+                file.createNewFile();
+            }
+
+            byte[] inBytes = new byte[1024*5];
+            int len = 0;
+            while (0 < (len = inputStream.read(inBytes, 0, inBytes.length))) {
+                fop.write(inBytes, 0, len);
+                fop.flush();
+            }
+            return true;
+        } catch (IOException e) {
+            e.printStackTrace();
+            return false;
+        } finally {
+            try {
+                if (fop != null) {
+                    fop.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+            try {
+                if (inputStream != null) {
+                    inputStream.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    /**
+     * 鏂囦欢鏄惁瀛樺湪
+     *
+     * @param file
+     * @return
+     */
+    public static boolean isFileExists(File file) {
+        if (file == null)
+            return false;
+        return file.exists();
+    }
+
+    /**
+     * 鏂囦欢澶规槸鍚﹀瓨鍦�
+     *
+     * @param file
+     * @return
+     */
+    public static boolean isDirectory(File file) {
+        if (file == null)
+            return false;
+        return file.isDirectory();
+    }
+    public static void byteToFile(byte[] buf, String filePath, String fileName) {
+        BufferedOutputStream bos = null;
+        FileOutputStream fos = null;
+        File file;
+        try {
+            createOrExistsPrivateFolder(filePath);
+            file = new File(filePath + File.separator + fileName);
+            fos = new FileOutputStream(file);
+            bos = new BufferedOutputStream(fos);
+            bos.write(buf);
+        } catch (Exception ignored) {
+        } finally {
+            if (bos != null) {
+                try {
+                    bos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (fos != null) {
+                try {
+                    fos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+
+    /**
+     * 鍒犻櫎鏂囦欢锛屽彲浠ユ槸鍗曚釜鏂囦欢鎴栨枃浠跺す
+     *
+     * @param fileName
+     *            寰呭垹闄ょ殑鏂囦欢鍚�
+     * @return 鏂囦欢鍒犻櫎鎴愬姛杩斿洖true,鍚﹀垯杩斿洖false
+     */
+    public static boolean delete(String fileName) {
+        File file = new File(fileName);
+        if (!file.exists()) {
+            System.out.println("鍒犻櫎鏂囦欢澶辫触锛�" + fileName + "鏂囦欢涓嶅瓨鍦�");
+            return false;
+        } else {
+            if (file.isFile()) {
+
+                return deleteFile(fileName);
+            } else {
+                return deleteDirectory(fileName);
+            }
+        }
+    }
+
+    /**
+     * 鍒犻櫎鍗曚釜鏂囦欢
+     *
+     * @param file 琚垹闄ゆ枃浠�
+     * @return 鍗曚釜鏂囦欢鍒犻櫎鎴愬姛杩斿洖true,鍚﹀垯杩斿洖false
+     */
+    public static boolean deleteFile(File file) {
+        if (file.isFile() && file.exists()) {
+            file.delete();
+            System.out.println("鍒犻櫎鍗曚釜鏂囦欢" + file.getName() + "鎴愬姛锛�");
+            return true;
+        } else {
+            System.out.println("鍒犻櫎鍗曚釜鏂囦欢" + file.getName() + "澶辫触锛�");
+            return false;
+        }
+    }
+
+    /**
+     * 鍒犻櫎鍗曚釜鏂囦欢
+     *
+     * @param fileName
+     *            琚垹闄ゆ枃浠剁殑鏂囦欢鍚�
+     * @return 鍗曚釜鏂囦欢鍒犻櫎鎴愬姛杩斿洖true,鍚﹀垯杩斿洖false
+     */
+    public static boolean deleteFile(String fileName) {
+        File file = new File(fileName);
+        if (file.isFile() && file.exists()) {
+            file.delete();
+            System.out.println("鍒犻櫎鍗曚釜鏂囦欢" + fileName + "鎴愬姛锛�");
+            return true;
+        } else {
+            System.out.println("鍒犻櫎鍗曚釜鏂囦欢" + fileName + "澶辫触锛�");
+            return false;
+        }
+    }
+
+    /**
+     * 鍒犻櫎鐩綍锛堟枃浠跺す锛変互鍙婄洰褰曚笅鐨勬枃浠�
+     *
+     * @param dir
+     *            琚垹闄ょ洰褰曠殑鏂囦欢璺緞
+     * @return 鐩綍鍒犻櫎鎴愬姛杩斿洖true,鍚﹀垯杩斿洖false
+     */
+    public static boolean deleteDirectory(String dir) {
+        // 濡傛灉dir涓嶄互鏂囦欢鍒嗛殧绗︾粨灏撅紝鑷姩娣诲姞鏂囦欢鍒嗛殧绗�
+        if (!dir.endsWith(File.separator)) {
+            dir = dir + File.separator;
+        }
+        File dirFile = new File(dir);
+        // 濡傛灉dir瀵瑰簲鐨勬枃浠朵笉瀛樺湪锛屾垨鑰呬笉鏄竴涓洰褰曪紝鍒欓��鍑�
+        if (!dirFile.exists() || !dirFile.isDirectory()) {
+            System.out.println("鍒犻櫎鐩綍澶辫触" + dir + "鐩綍涓嶅瓨鍦紒");
+            return false;
+        }
+        boolean flag = true;
+        // 鍒犻櫎鏂囦欢澶逛笅鐨勬墍鏈夋枃浠�(鍖呮嫭瀛愮洰褰�)
+        File[] files = dirFile.listFiles();
+        for (int i = 0; i < files.length; i++) {
+            // 鍒犻櫎瀛愭枃浠�
+            if (files[i].isFile()) {
+                flag = deleteFile(files[i].getAbsolutePath());
+                if (!flag) {
+                    break;
+                }
+            }
+            // 鍒犻櫎瀛愮洰褰�
+            else {
+                flag = deleteDirectory(files[i].getAbsolutePath());
+                if (!flag) {
+                    break;
+                }
+            }
+        }
+
+        if (!flag) {
+            System.out.println("鍒犻櫎鐩綍澶辫触");
+            return false;
+        }
+
+        // 鍒犻櫎褰撳墠鐩綍
+        if (dirFile.delete()) {
+            System.out.println("鍒犻櫎鐩綍" + dir + "鎴愬姛锛�");
+            return true;
+        } else {
+            System.out.println("鍒犻櫎鐩綍" + dir + "澶辫触锛�");
+            return false;
+        }
+    }
+    // 鍒犻櫎鏂囦欢澶�
+    // param folderPath 鏂囦欢澶瑰畬鏁寸粷瀵硅矾寰�
+
+    public static void delFolder(String folderPath) {
+        try {
+            delAllFile(folderPath); // 鍒犻櫎瀹岄噷闈㈡墍鏈夊唴瀹�
+            String filePath = folderPath;
+            filePath = filePath.toString();
+            File myFilePath = new File(filePath);
+            myFilePath.delete(); // 鍒犻櫎绌烘枃浠跺す
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    // 鍒犻櫎鎸囧畾鏂囦欢澶逛笅鎵�鏈夋枃浠�
+    // param path 鏂囦欢澶瑰畬鏁寸粷瀵硅矾寰�
+    public static boolean delAllFile(String path) {
+        boolean flag = false;
+        File file = new File(path);
+        if (!file.exists()) {
+            return flag;
+        }
+        if (!file.isDirectory()) {
+            return flag;
+        }
+        String[] tempList = file.list();
+        File temp = null;
+        for (int i = 0; i < tempList.length; i++) {
+            if (path.endsWith(File.separator)) {
+                temp = new File(path + tempList[i]);
+            } else {
+                temp = new File(path + File.separator + tempList[i]);
+            }
+            if (temp.isFile()) {
+                temp.delete();
+            }
+            if (temp.isDirectory()) {
+                delAllFile(path + "/" + tempList[i]);// 鍏堝垹闄ゆ枃浠跺す閲岄潰鐨勬枃浠�
+                delFolder(path + "/" + tempList[i]);// 鍐嶅垹闄ょ┖鏂囦欢澶�
+                flag = true;
+            }
+        }
+        return flag;
+    }
+}
+

--
Gitblit v1.8.0