| | |
| | | |
| | | import android.graphics.Bitmap; |
| | | import android.graphics.BitmapFactory; |
| | | import android.text.TextUtils; |
| | | import android.util.Log; |
| | | |
| | | import com.hdl.photovoltaic.HDLApp; |
| | |
| | | import java.io.IOException; |
| | | |
| | | /** |
| | | * 文件的逻辑 |
| | | * 文件操作的逻辑 |
| | | */ |
| | | public class HdlFileLogic { |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取存放文件用户根路径 |
| | | * 获取存放文件【用户】根路径 |
| | | */ |
| | | public String getUserFilesPath() { |
| | | return HDLApp.getInstance().getFilesDir().getAbsolutePath() + getUserId(); |
| | | } |
| | | |
| | | /** |
| | | * 获取存放文件住宅根路径 |
| | | * 获取存放文件【住宅】根路径 |
| | | */ |
| | | public String getHomeFilesPath() { |
| | | return HDLApp.getInstance().getFilesDir().getAbsolutePath() + getUserId() + "/home"; |
| | |
| | | return getHomeId() + ".json"; |
| | | } |
| | | |
| | | public File createFile(String dirPath, String fileName) { |
| | | /** |
| | | * 创建文件 |
| | | * |
| | | * @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; |
| | | } |
| | | } |
| | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 读取文件 |
| | | * |
| | | * @param filepath 全路径 |
| | | * @return 数据 |
| | | */ |
| | | public String readFile(String filepath) { |
| | | try { |
| | | String fileContent = ""; |
| | |
| | | //关闭缓存输入流 |
| | | bis.close(); |
| | | return bitmap; |
| | | |
| | | |
| | | } catch (Exception e) { |
| | | } |
| | | return null; |
| | |
| | | */ |
| | | public void createDirectory() { |
| | | //用户信息 |
| | | this.createFileDir(new File(this.getUserFilesPath())); |
| | | this.createFileDir(this.getUserFilesPath()); |
| | | //住宅信息 |
| | | this.createFileDir(new File(this.getHomeFilesPath())); |
| | | 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; |
| | | } |
| | | |
| | | } |
| | | } |