From 975b91521a04e159f45fb34fc7b55afbf455f7f5 Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期三, 28 六月 2023 16:53:34 +0800 Subject: [PATCH] 2023年06月28日16:53:33 --- app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java | 217 ++++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 154 insertions(+), 63 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 958fb8b..5b2ddd3 100644 --- a/app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java +++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java @@ -2,19 +2,20 @@ 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.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.IOException; /** - * 鏂囦欢鐣岄潰鐨勯�昏緫 + * 鏂囦欢鎿嶄綔鐨勯�昏緫 */ public class HdlFileLogic { @@ -39,84 +40,165 @@ 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(); + } + //region ---------root璺緞----------- /** - * 鑾峰彇鍐呴儴瀛樺偍鏂囦欢璺緞 + * 鑾峰彇鎵嬫満鍐呴儴瀛樺偍鏂囦欢璺緞 */ - private String getInternalStoreFilesPath() { - return HDLApp.getInstance().getFilesDir().getAbsolutePath(); + private String getAPPInternalStoreFilesPath() { + return HDLApp.getInstance().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).getPath(); } /** - * 鑾峰彇App瀛樻斁鏂囦欢鐨勬牴璺緞 + * 鑾峰彇瀛樻斁鏂囦欢銆愮敤鎴枫�戞牴璺緞 */ - public String getAppFilesPath() { - return HDLApp.getInstance().getFilesDir().getAbsolutePath() + userId + "/home"; + public String getCurrentUserRootPath() { + return getAPPInternalStoreFilesPath() + "/" + getUserId(); } - public File createFile(String dirPath, String fileName) { + /** + * 鑾峰彇瀛樻斁鏂囦欢銆愪綇瀹呫�戞牴璺緞 + */ + public String getCurrentHomeRootPath() { + return getAPPInternalStoreFilesPath() + "/" + getUserId() + "/home"; + } + + /** + * 浣忓畢鏂囦欢鍚嶇О + * + * @return - + */ + public String getCurrentHomeFileName() { + return "/" + getHomeId() + ".json"; + } + + + /** + * 鑾峰彇褰撳墠浣忓畢鏂囦欢鍏ㄨ矾寰� + */ + public String getCurrentHomeFilesFullPath() { + return getCurrentHomeRootPath() + getCurrentHomeFileName(); + } + + //endregion + + //region ---------銆愭枃浠跺す銆戞搷浣�----------- + + /** + * 棰勫垱寤烘枃浠跺す + */ + public void createDirectory() { + //瀛樻斁鐢ㄦ埛淇℃伅 + this.createFileDir(this.getCurrentUserRootPath()); + //瀛樻斁浣忓畢淇℃伅 + this.createFileDir(this.getCurrentHomeRootPath()); + } + + /** + * 鍒涘缓鏂囦欢澶� + * + * @param fullPath fullPath 鍏ㄨ矾寰� + * @return - + */ + public boolean createFileDir(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(fullPath); + if (!file.isDirectory()) { + boolean succeed = file.mkdirs(); + HdlLogLogic.print("鍒涘缓鏂囦欢澶�==" + succeed); + return succeed; } - File file = new File(dirPath, fileName); - if (!file.exists()) { - if (!file.createNewFile()) { - Log.e(TAG, "createFile createNewFile fail"); - return null; - } - } - 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 鍏ㄨ矾寰� + */ + 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 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 deleteFile(String fullPath) { + try { + File file = new File(fullPath); + if (file.exists()) { + boolean succeed = file.delete(); + 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; } } @@ -124,12 +206,19 @@ d.write(data.getBytes()); d.flush(); d.close(); - } catch (IOException e) { - e.printStackTrace(); + HdlLogLogic.print("鍐欏叆鏂囦欢鎴愬姛==" + fullPath); + } catch (Exception e) { + HdlLogLogic.print("鍐欏叆鏂囦欢鏈夊紓甯�==" + e.getMessage()); } } + /** + * 璇诲彇鏂囦欢 + * + * @param filepath 鍏ㄨ矾寰� + * @return 鏁版嵁 + */ public String readFile(String filepath) { try { String fileContent = ""; @@ -146,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 /** * 鎶婁綅鍥炬暟鎹繚瀛樺埌鎸囧畾璺緞鐨勫浘鐗囨枃浠� @@ -174,6 +264,7 @@ } catch (Exception e) { } } + //</editor-fold> /** * 浠庢寚瀹氳矾寰勭殑鍥剧墖鏂囦欢涓鍙栦綅鍥炬暟鎹� @@ -187,10 +278,10 @@ //鍏抽棴缂撳瓨杈撳叆娴� bis.close(); return bitmap; - - } catch (Exception e) { } return null; } + + } -- Gitblit v1.8.0