New file |
| | |
| | | package com.hdl.photovoltaic.other; |
| | | |
| | | import android.graphics.Bitmap; |
| | | import android.graphics.BitmapFactory; |
| | | import android.util.Log; |
| | | |
| | | import com.hdl.photovoltaic.HDLApp; |
| | | |
| | | 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"; |
| | | |
| | | public static final String userId = ""; |
| | | |
| | | /** |
| | | * 获取内部存储文件路径 |
| | | */ |
| | | private String getInternalStoreFilesPath() { |
| | | return HDLApp.getInstance().getFilesDir().getAbsolutePath(); |
| | | } |
| | | |
| | | /** |
| | | * 获取App存放文件的根路径 |
| | | */ |
| | | public String getAppFilesPath() { |
| | | return HDLApp.getInstance().getFilesDir().getAbsolutePath() + userId + "/home"; |
| | | } |
| | | |
| | | public File createFile(String dirPath, String fileName) { |
| | | 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); |
| | | if (!file.exists()) { |
| | | if (!file.createNewFile()) { |
| | | Log.e(TAG, "createFile createNewFile fail"); |
| | | return null; |
| | | } |
| | | } |
| | | return file; |
| | | } catch (Exception e) { |
| | | HdlLogLogic.print(TAG, "createFile fail :" + e.getMessage()); |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 创建文件夹---之所以要一层层创建,是因为一次性创建多层文件夹可能会失败! |
| | | */ |
| | | 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); |
| | | } |
| | | return isSuccess; |
| | | } |
| | | } |
| | | |
| | | public void writeFile(String path, String data) { |
| | | try { |
| | | File file = new File(path); |
| | | if (!file.exists()) { |
| | | if (!file.mkdir() && !file.isDirectory()) { |
| | | HdlLogLogic.print(TAG, "Error: make dir failed!"); |
| | | return; |
| | | } |
| | | } |
| | | FileOutputStream d = new FileOutputStream(file); |
| | | d.write(data.getBytes()); |
| | | d.flush(); |
| | | d.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | } |