From 60f74b306659cba1a8ed7378f9df54a71e48a614 Mon Sep 17 00:00:00 2001 From: hxb <hxb@hdlchina.com.cn> Date: 星期三, 28 六月 2023 18:04:09 +0800 Subject: [PATCH] 临时 --- app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java | 187 ++++++++++++++++++++++++++++++++-------------- 1 files changed, 131 insertions(+), 56 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..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,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; @@ -14,7 +16,7 @@ import java.io.IOException; /** - * 鏂囦欢鐣岄潰鐨勯�昏緫 + * 鏂囦欢鎿嶄綔鐨勯�昏緫 */ public class HdlFileLogic { @@ -39,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; } } @@ -130,6 +147,12 @@ } + /** + * 璇诲彇鏂囦欢 + * + * @param filepath 鍏ㄨ矾寰� + * @return 鏁版嵁 + */ public String readFile(String filepath) { try { String fileContent = ""; @@ -187,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; + } + + } } -- Gitblit v1.8.0