package com.hdl.photovoltaic.other; 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; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * 文件操作的逻辑 */ public class HdlFileLogic { private static volatile HdlFileLogic sHdlFileLogic; /** * 获取当前对象 * * @return HdlFileLogic */ public static synchronized HdlFileLogic getInstance() { if (sHdlFileLogic == null) { synchronized (HdlFileLogic.class) { if (sHdlFileLogic == null) { sHdlFileLogic = new HdlFileLogic(); } } } return sHdlFileLogic; } private static final String TAG = "FileUtils"; private String getUserId() { return UserConfigManage.getInstance().getUserId(); } private String getHomeId() { return UserConfigManage.getInstance().getHomeId(); } /** * 获取手机内部存储文件路径 */ private String getAPPInternalStoreFilesPath() { return HDLApp.getInstance().getFilesDir().getAbsolutePath(); } /** * 获取存放文件【用户】根路径 */ public String getUserFilesPath() { return HDLApp.getInstance().getFilesDir().getAbsolutePath() + getUserId(); } /** * 获取存放文件【住宅】根路径 */ 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 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 fullPath 全路径 * @return - */ public boolean createFileDir(String fullPath) { try { File file = new File(fullPath); if (!file.isDirectory()) { boolean succeed = file.mkdirs(); HdlLogLogic.print("创建文件夹==" + succeed); return succeed; } return true; } catch (Exception e) { HdlLogLogic.print("创建文件夹失败==" + e.getMessage()); return false; } } /** * 写入文件 * * @param fullPath 全路径 * @param data 数据 */ public void writeFile(String fullPath, String data) { try { File file = new File(fullPath); if (!file.exists()) { if (!createFile(fullPath)) { //创建失败直接返回 return; } } FileOutputStream d = new FileOutputStream(file); d.write(data.getBytes()); d.flush(); d.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 读取文件 * * @param filepath 全路径 * @return 数据 */ public String readFile(String filepath) { try { String fileContent = ""; if (null == filepath) { Log.d(TAG, "Error: Invalid file name!"); return fileContent; } File f = new File(filepath); if (!f.exists()) { return fileContent; } FileInputStream fis = new FileInputStream(f); byte[] bytes = new byte[fis.available()]; fis.read(bytes); fis.close(); fileContent = new String(bytes); return fileContent; } catch (Exception e1) { e1.printStackTrace(); Log.d(TAG, "Error: Input File not find!"); return ""; } } /** * 把位图数据保存到指定路径的图片文件 */ public void writeImage(String path, Bitmap bitmap) { try { //根据指定文件路径构建缓存输出流对象 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path)); //把位图数据压缩到缓存输出流中 bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos); //完成缓存输出流的写入动作 bos.flush(); //关闭缓存输出流 bos.close(); } catch (Exception e) { } } /** * 从指定路径的图片文件中读取位图数据 */ public Bitmap readImage(String path) { try { //根据指定文件路径构建缓存输入流对象 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path)); //从缓存输入流中解码位图数据 Bitmap bitmap = BitmapFactory.decodeStream(bis); //关闭缓存输入流 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; } } }