From 2c7615cd73dfa6a7ca4df975430d2217524513d2 Mon Sep 17 00:00:00 2001
From: mac <user@users-MacBook-Pro.local>
Date: 星期四, 28 九月 2023 11:38:28 +0800
Subject: [PATCH] 2023年09月28日11:38:24
---
app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java | 208 +++++++++++++++++++++++++++++++++------------------
1 files changed, 133 insertions(+), 75 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..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,6 +2,7 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
+import android.os.Environment;
import android.util.Log;
import com.hdl.photovoltaic.HDLApp;
@@ -12,10 +13,9 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
-import java.io.IOException;
/**
- * 鏂囦欢鐨勯�昏緫
+ * 鏂囦欢鎿嶄綔鐨勯�昏緫
*/
public class HdlFileLogic {
@@ -47,99 +47,158 @@
private String getHomeId() {
return UserConfigManage.getInstance().getHomeId();
}
+ //region ---------root璺緞-----------
/**
* 鑾峰彇鎵嬫満鍐呴儴瀛樺偍鏂囦欢璺緞
*/
private String getAPPInternalStoreFilesPath() {
- return HDLApp.getInstance().getFilesDir().getAbsolutePath();
+ return HDLApp.getInstance().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).getPath();
}
/**
- * 鑾峰彇瀛樻斁鏂囦欢鐢ㄦ埛鏍硅矾寰�
+ * 鑾峰彇瀛樻斁鏂囦欢銆愮敤鎴枫�戞牴璺緞
*/
- public String getUserFilesPath() {
- return HDLApp.getInstance().getFilesDir().getAbsolutePath() + getUserId();
+ public String getCurrentUserRootPath() {
+ return getAPPInternalStoreFilesPath() + "/" + getUserId();
}
/**
- * 鑾峰彇瀛樻斁鏂囦欢浣忓畢鏍硅矾寰�
+ * 鑾峰彇瀛樻斁鏂囦欢銆愪綇瀹呫�戞牴璺緞
*/
- public String getHomeFilesPath() {
- return HDLApp.getInstance().getFilesDir().getAbsolutePath() + getUserId() + "/home";
+ public String getCurrentHomeRootPath() {
+ return getAPPInternalStoreFilesPath() + "/" + getUserId() + "/home";
}
/**
- * 浣忓畢鍚嶇О
+ * 浣忓畢鏂囦欢鍚嶇О
*
* @return -
*/
- public String getHomeFileName() {
- return getHomeId() + ".json";
+ public String getCurrentHomeFileName() {
+ return "/" + getHomeId() + ".json";
}
- public File createFile(String dirPath, String fileName) {
+
+ /**
+ * 鑾峰彇褰撳墠浣忓畢鏂囦欢鍏ㄨ矾寰�
+ */
+ 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;
}
}
@@ -147,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 = "";
@@ -169,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
/**
* 鎶婁綅鍥炬暟鎹繚瀛樺埌鎸囧畾璺緞鐨勫浘鐗囨枃浠�
@@ -197,6 +264,7 @@
} catch (Exception e) {
}
}
+ //</editor-fold>
/**
* 浠庢寚瀹氳矾寰勭殑鍥剧墖鏂囦欢涓鍙栦綅鍥炬暟鎹�
@@ -210,20 +278,10 @@
//鍏抽棴缂撳瓨杈撳叆娴�
bis.close();
return bitmap;
-
-
} catch (Exception e) {
}
return null;
}
- /**
- * 棰勫垱寤烘枃浠跺す
- */
- public void createDirectory() {
- //鐢ㄦ埛淇℃伅
- this.createFileDir(new File(this.getUserFilesPath()));
- //浣忓畢淇℃伅
- this.createFileDir(new File(this.getHomeFilesPath()));
- }
+
}
--
Gitblit v1.8.0