From 9e875b253959eff8f6af567308ad804fe69d0a62 Mon Sep 17 00:00:00 2001 From: mac <user@users-MacBook-Pro.local> Date: 星期五, 03 十一月 2023 18:04:57 +0800 Subject: [PATCH] 2023年11月03日18:04:44 --- app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java | 181 +++++++++++++++++++++++++++++++++++--------- 1 files changed, 142 insertions(+), 39 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 78760e0..b0b7b8f 100644 --- a/app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java +++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java @@ -3,16 +3,19 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Environment; -import android.util.Log; import com.hdl.photovoltaic.HDLApp; import com.hdl.photovoltaic.config.UserConfigManage; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; +import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Objects; /** * 鏂囦欢鎿嶄綔鐨勯�昏緫 @@ -20,6 +23,11 @@ public class HdlFileLogic { private static volatile HdlFileLogic sHdlFileLogic; + + /** + * 琛ㄧず1m鏁版嵁 + */ + private final int m = 1; /** * 鑾峰彇褰撳墠瀵硅薄 @@ -57,34 +65,31 @@ } /** - * 鑾峰彇瀛樻斁鏂囦欢銆愮敤鎴枫�戞牴璺緞 + * 鑾峰彇瀛樻斁銆愮敤鎴枫�戞枃浠跺す鏍硅矾寰� */ public String getCurrentUserRootPath() { return getAPPInternalStoreFilesPath() + "/" + getUserId(); } /** - * 鑾峰彇瀛樻斁鏂囦欢銆愪綇瀹呫�戞牴璺緞 + * 鑾峰彇瀛樻斁銆愪綇瀹呫�戞枃浠跺す鏍硅矾寰� */ public String getCurrentHomeRootPath() { - return getAPPInternalStoreFilesPath() + "/" + getUserId() + "/home"; + return getCurrentUserRootPath() + "/" + "home_" + getHomeId(); } /** - * 浣忓畢鏂囦欢鍚嶇О - * - * @return - + * 鑾峰彇銆愭棩蹇椼�戞枃浠跺叏璺緞 */ - public String getCurrentHomeFileName() { - return "/" + getHomeId() + ".json"; + public String getLogFilePath() { + return getCurrentHomeRootPath() + "/Log.txt"; } - /** - * 鑾峰彇褰撳墠浣忓畢鏂囦欢鍏ㄨ矾寰� + * 鑾峰彇銆愮敤鎴枫�戞枃浠跺叏璺緞 */ - public String getCurrentHomeFilesFullPath() { - return getCurrentHomeRootPath() + getCurrentHomeFileName(); + public String getUserFilePath() { + return getAPPInternalStoreFilesPath() + "/UserConfigManage.txt"; } //endregion @@ -95,8 +100,6 @@ * 棰勫垱寤烘枃浠跺す */ public void createDirectory() { - //瀛樻斁鐢ㄦ埛淇℃伅 - this.createFileDir(this.getCurrentUserRootPath()); //瀛樻斁浣忓畢淇℃伅 this.createFileDir(this.getCurrentHomeRootPath()); } @@ -112,12 +115,12 @@ File file = new File(fullPath); if (!file.isDirectory()) { boolean succeed = file.mkdirs(); - HdlLogLogic.print("鍒涘缓鏂囦欢澶�==" + succeed); + System.out.println("鍒涘缓鏂囦欢澶硅矾寰�=" + file.getAbsolutePath() + "->->鍒涘缓鏂囦欢澶圭粨鏋�=" + succeed); return succeed; } return true; } catch (Exception e) { - HdlLogLogic.print("鍒涘缓鏂囦欢澶规湁寮傚父==" + e.getMessage()); + System.out.println("鍒涘缓鏂囦欢澶规湁寮傚父===" + e.getMessage()); return false; } } @@ -130,16 +133,29 @@ */ public void deleteDirectory(String fullPath) { try { - File file = new File(fullPath); - if (file.isDirectory()) { - boolean succeed = file.delete(); - HdlLogLogic.print("鍒犻櫎鏂囦欢澶�==" + succeed); + File fileRoot = new File(fullPath); + if (fileRoot.isDirectory()) { + File[] listFiles = fileRoot.listFiles(); + if (listFiles != null) { + for (File file : listFiles) { + if (file.isDirectory()) { + deleteDirectory(file.getAbsolutePath()); + } else { + this.deleteFile(file.getAbsolutePath()); + } + } + } } + // 鍒犻櫎鏂囦欢澶规湰韬� + boolean succeed = fileRoot.delete();//鏂囦欢澶圭┖杩欎釜鏂规硶鎵嶆湁鏁� + System.out.println("鍒犻櫎鏂囦欢澶硅矾寰�=" + fileRoot.getAbsolutePath() + "->->鍒犻櫎缁撴灉=" + succeed); } catch (Exception e) { - HdlLogLogic.print("鍒犻櫎鏂囦欢澶规湁寮傚父==" + e.getMessage()); + System.out.println("鍒犻櫎鏂囦欢澶规湁寮傚父===" + e.getMessage()); } } + + //endregion //region ---------銆愭枃浠躲�戞搷浣�----------- @@ -155,12 +171,12 @@ File file = new File(fullPath); if (!file.exists()) { boolean succeed = file.createNewFile(); - HdlLogLogic.print("鍒涘缓鏂囦欢==" + succeed); + System.out.println("鍒涘缓鏂囦欢璺緞=" + file.getAbsolutePath() + "->->鍒涘缓鏂囦欢缁撴灉=" + succeed); return succeed; } return true; } catch (Exception e) { - HdlLogLogic.print("鍒涘缓鏂囦欢鏈夊紓甯�==" + e.getMessage()); + System.out.println("鍒涘缓鏂囦欢鏈夊紓甯�===" + e.getMessage()); return false; } } @@ -176,16 +192,17 @@ File file = new File(fullPath); if (file.exists()) { boolean succeed = file.delete(); - HdlLogLogic.print("鍒犻櫎鏂囦欢==" + succeed); + System.out.println("鍒犻櫎鏂囦欢===" + succeed); return succeed; } return true; } catch (Exception e) { - HdlLogLogic.print("鍒犻櫎鏂囦欢鏈夊紓甯�==" + e.getMessage()); + System.out.println("鍒犻櫎鏂囦欢鏈夊紓甯�===" + e.getMessage()); return false; } } + /** * 鍐欏叆鏂囦欢 @@ -206,9 +223,43 @@ d.write(data.getBytes()); d.flush(); d.close(); - HdlLogLogic.print("鍐欏叆鏂囦欢鎴愬姛==" + fullPath); + System.out.println("鍐欏叆鏂囦欢鎴愬姛==" + fullPath); } catch (Exception e) { - HdlLogLogic.print("鍐欏叆鏂囦欢鏈夊紓甯�==" + e.getMessage()); + System.out.println("鍐欏叆鏂囦欢鏈夊紓甯�==" + e.getMessage()); + } + + } + + /** + * 鍐欏叆鏂囦欢(涓�琛屼竴琛岃拷鍔犲唴瀹�) + * + * @param fullPath 鍏ㄨ矾寰� + * @param dataLine 鏁版嵁 + */ + public void appendFile(String fullPath, String dataLine) { + try { + if (!isBoolean(fullPath)) { + return; + } + File file = new File(fullPath); + if (!file.exists()) { + if (!createFile(fullPath)) { + //鍒涘缓澶辫触鐩存帴杩斿洖 + return; + } + } + FileOutputStream d = new FileOutputStream(file, true); + d.write(dataLine.getBytes()); + d.write("\r\n".getBytes());// 鍐欏叆涓�涓崲琛� + d.flush(); + d.close(); + if (file.length() > 1024 * 1024 * m) { + //鏂囦欢澶т簬1m,鍒犻櫎鏂囦欢鍓�10鏉℃棩蹇� + this.delFileLien(fullPath, 10); + } + System.out.println("鍐欏叆涓�琛屾暟鎹埌鏂囦欢鎴愬姛===" + dataLine); + } catch (Exception e) { + System.out.println("鍐欏叆涓�琛屾暟鎹埌鏂囦欢鏈夊紓甯�===" + e.getMessage()); } } @@ -216,18 +267,16 @@ /** * 璇诲彇鏂囦欢 * - * @param filepath 鍏ㄨ矾寰� + * @param filePath 鍏ㄨ矾寰� * @return 鏁版嵁 */ - public String readFile(String filepath) { + public String readFile(String filePath) { try { - String fileContent = ""; - if (null == filepath) { - Log.d(TAG, "Error: Invalid file name!"); - return fileContent; + if (!isBoolean(filePath)) { + return ""; } - - File f = new File(filepath); + String fileContent = ""; + File f = new File(filePath); if (!f.exists()) { return fileContent; } @@ -235,14 +284,68 @@ byte[] bytes = new byte[fis.available()]; fis.read(bytes); fis.close(); - HdlLogLogic.print("璇诲彇鏂囦欢鎴愬姛==" + filepath); + System.out.println("璇诲彇鏂囦欢鎴愬姛===" + filePath); fileContent = new String(bytes); return fileContent; } catch (Exception e1) { - HdlLogLogic.print("璇诲彇鏂囦欢鏈夊紓甯�==" + e1.getMessage()); + System.out.println("璇诲彇鏂囦欢鏈夊紓甯�===" + e1.getMessage()); return ""; } + } + + /** + * 鎸囧畾鍒犻櫎鏂囦欢琛屾暟锛堜粠鍓嶉潰鍒犻櫎璧凤級 + * + * @param filePath 璺緞 + * @param delLienSum 鍒犻櫎澶氬皯琛屾暟 + */ + public void delFileLien(String filePath, int delLienSum) { + if (!isBoolean(filePath)) { + return; + } + //鎵撳紑鏂囦欢 + File file = new File(filePath); + //濡傛灉path鏄紶閫掕繃鏉ョ殑鍙傛暟锛屽彲浠ュ仛涓�涓潪鐩綍鐨勫垽鏂� + if (file.isDirectory()) { + System.out.println("杩欐槸涓�涓洰褰�"); + return; + } + StringBuilder content = new StringBuilder(); //鏂囦欢鍐呭瀛楃涓� + try { + InputStream inputStream = new FileInputStream(file); + InputStreamReader inputStreamReader = new InputStreamReader(inputStream); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + String line; + int lineCount = 0; + //鍒嗚璇诲彇 + while ((line = bufferedReader.readLine()) != null) { + lineCount += 1; + if (lineCount > delLienSum) { + content.append(line).append("\r\n"); + } + } + inputStream.close(); + //鍒犻櫎鎺夋棫鏂囦欢 + this.deleteFile(filePath); + //閲嶆柊鍒涘缓鏂囦欢骞朵笖鍐欏叆鏁版嵁 + this.writeFile(filePath, content.toString()); + } catch (Exception ignored) { + } + } + + /** + * 鍒ゆ柇璺緞鏄惁鍚堟硶 + * + * @param path 璺緞 + * @return true鍚堟硶 + */ + public boolean isBoolean(String path) { + if (path.contains("//") || path.contains("\\")) { + System.out.println("鏃犳晥鏂囦欢璺緞===" + path); + return false; + } + return true; } //endregion @@ -261,7 +364,7 @@ bos.close(); - } catch (Exception e) { + } catch (Exception ignored) { } } //</editor-fold> -- Gitblit v1.8.0