FileUtil:file 工具類
阿新 • • 發佈:2018-11-11
import android.content.Context; import android.os.Environment; import com.example.myresumemvvm.MyApplication; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.RandomAccessFile; import java.util.HashMap; import java.util.Map; import java.util.Properties; /** * @author 寫檔案的工具類 */ public class FileUtil { public static final String ROOT_DIR = "Android/data/" + CommonUtil.getPackageName(); public static final String DOWNLOAD_DIR = "download"; public static final String CACHE_DIR = "DOWNLOAD_INFO_HASHMAP"; public static final String ICON_DIR = "icon"; private static Context getContext(){ return MyApplication.context; } /** * 獲取下載目錄 */ public static String getDownloadDir() { return getDir(DOWNLOAD_DIR); } /** * 獲取快取目錄 */ public static String getCacheDir() { return getDir(CACHE_DIR); } /** * 獲取icon目錄 */ public static String getIconDir() { return getDir(ICON_DIR); } /** * 判斷SD卡是否掛載 */ public static boolean isSDCardAvailable() { if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { return true; } else { return false; } } /** * 建立資料夾 */ public static boolean createDirs(String dirPath) { File file = new File(dirPath); if (!file.exists() || !file.isDirectory()) { return file.mkdirs(); } return true; } /** * 複製檔案,可以選擇是否刪除原始檔 */ public static boolean copyFile(String srcPath, String destPath, boolean deleteSrc) { File srcFile = new File(srcPath); File destFile = new File(destPath); return copyFile(srcFile, destFile, deleteSrc); } /** * 複製檔案,可以選擇是否刪除原始檔 */ public static boolean copyFile(File srcFile, File destFile, boolean deleteSrc) { if (!srcFile.exists() || !srcFile.isFile()) { return false; } InputStream in = null; OutputStream out = null; try { in = new FileInputStream(srcFile); out = new FileOutputStream(destFile); byte[] buffer = new byte[1024]; int i = -1; while ((i = in.read(buffer)) > 0) { out.write(buffer, 0, i); out.flush(); } if (deleteSrc) { srcFile.delete(); } } catch (Exception e) { LogUtil.e(e); return false; } finally { IOUtils.close(out); IOUtils.close(in); } return true; } /** * 獲取應用目錄,當SD卡存在時,獲取SD卡上的目錄,當SD卡不存在時,獲取應用的cache目錄 */ public static String getDir(String name) { StringBuilder sb = new StringBuilder(); if (isSDCardAvailable()) { sb.append(getExternalStoragePath()); } else { sb.append(getCachePath()); } sb.append(name); sb.append(File.separator); String path = sb.toString(); if (createDirs(path)) { return path; } else { return null; } } /** * 獲取SD下的應用目錄 */ public static String getExternalStoragePath() { StringBuilder sb = new StringBuilder(); sb.append(Environment.getExternalStorageDirectory().getAbsolutePath()); sb.append(File.separator); sb.append(ROOT_DIR); sb.append(File.separator); return sb.toString(); } /** * 獲取應用的cache目錄 */ public static String getCachePath() { File f = getContext().getCacheDir(); if (null == f) { return null; } else { return f.getAbsolutePath() + "/"; } } /** * 獲取下載後的apk檔案 * * @param dir(所在資料夾) * @param packageName(apk包名) * @return */ public static File getApk(String dir, String packageName) { StringBuffer pathBuffer = new StringBuffer(getDir(dir)); pathBuffer.append(File.separator).append(packageName).append(".apk"); return new File(pathBuffer.toString()); } /** * 獲取下載後的apk * * @param packageName * @return */ public static File getApk(String packageName) { return getApk("apk", packageName); } /** * 依據包名刪除apk檔案 * @param packageName */ public static void deleteApk(String packageName) { File apk = getApk(packageName); if (apk != null && apk.exists()) { apk.delete(); } } /** * 判斷檔案是否可寫 */ public static boolean isWriteable(String path) { try { if (StringUtil.isEmpty(path)) { return false; } File f = new File(path); return f.exists() && f.canWrite(); } catch (Exception e) { LogUtil.e(e); return false; } } /** * 修改檔案的許可權,例如"777"等 */ public static void chmod(String path, String mode) { try { String command = "chmod " + mode + " " + path; Runtime runtime = Runtime.getRuntime(); runtime.exec(command); } catch (Exception e) { LogUtil.e(e); } } /** * 把資料寫入檔案 * * @param is 資料流 * @param path 檔案路徑 * @param recreate 如果檔案存在,是否需要刪除重建 * @return 是否寫入成功 */ public static boolean writeFile(InputStream is, String path, boolean recreate) { boolean res = false; File f = new File(path); FileOutputStream fos = null; try { if (recreate && f.exists()) { f.delete(); } if (!f.exists() && null != is) { File parentFile = new File(f.getParent()); parentFile.mkdirs(); int count = -1; byte[] buffer = new byte[1024]; fos = new FileOutputStream(f); while ((count = is.read(buffer)) != -1) { fos.write(buffer, 0, count); } res = true; } } catch (Exception e) { LogUtil.e(e); } finally { IOUtils.close(fos); IOUtils.close(is); } return res; } /** * 把字串資料寫入檔案 * * @param content 需要寫入的字串 * @param path 檔案路徑名稱 * @param append 是否以新增的模式寫入 * @return 是否寫入成功 */ public static boolean writeFile(byte[] content, String path, boolean append) { boolean res = false; File f = new File(path); RandomAccessFile raf = null; try { if (f.exists()) { if (!append) { f.delete(); f.createNewFile(); } } else { f.createNewFile(); } if (f.canWrite()) { raf = new RandomAccessFile(f, "rw"); raf.seek(raf.length()); raf.write(content); res = true; } } catch (Exception e) { LogUtil.e(e); } finally { IOUtils.close(raf); } return res; } /** * 把字串資料寫入檔案 * * @param content 需要寫入的字串 * @param path 檔案路徑名稱 * @param append 是否以新增的模式寫入 * @return 是否寫入成功 */ public static boolean writeFile(String content, String path, boolean append) { return writeFile(content.getBytes(), path, append); } /** * 把鍵值對寫入檔案 * * @param filePath 檔案路徑 * @param key 鍵 * @param value 值 * @param comment 該鍵值對的註釋 */ public static void writeProperties(String filePath, String key, String value, String comment) { if (StringUtil.isEmpty(key) || StringUtil.isEmpty(filePath)) { return; } FileInputStream fis = null; FileOutputStream fos = null; File f = new File(filePath); try { if (!f.exists() || !f.isFile()) { f.createNewFile(); } fis = new FileInputStream(f); Properties p = new Properties(); p.load(fis);// 先讀取檔案,再把鍵值對追加到後面 p.setProperty(key, value); fos = new FileOutputStream(f); p.store(fos, comment); } catch (Exception e) { LogUtil.e(e); } finally { IOUtils.close(fis); IOUtils.close(fos); } } /** * 根據值讀取 */ public static String readProperties(String filePath, String key, String defaultValue) { if (StringUtil.isEmpty(key) || StringUtil.isEmpty(filePath)) { return null; } String value = null; FileInputStream fis = null; File f = new File(filePath); try { if (!f.exists() || !f.isFile()) { f.createNewFile(); } fis = new FileInputStream(f); Properties p = new Properties(); p.load(fis); value = p.getProperty(key, defaultValue); } catch (IOException e) { LogUtil.e(e); } finally { IOUtils.close(fis); } return value; } /** * 把字串鍵值對的map寫入檔案 */ public static void writeMap(String filePath, Map<String, String> map, boolean append, String comment) { if (map == null || map.size() == 0 || StringUtil.isEmpty(filePath)) { return; } FileInputStream fis = null; FileOutputStream fos = null; File f = new File(filePath); try { if (!f.exists() || !f.isFile()) { f.createNewFile(); } Properties p = new Properties(); if (append) { fis = new FileInputStream(f); p.load(fis);// 先讀取檔案,再把鍵值對追加到後面 } p.putAll(map); fos = new FileOutputStream(f); p.store(fos, comment); } catch (Exception e) { LogUtil.e(e); } finally { IOUtils.close(fis); IOUtils.close(fos); } } /** * 把字串鍵值對的檔案讀入map */ @SuppressWarnings({"rawtypes", "unchecked"}) public static Map<String, String> readMap(String filePath, String defaultValue) { if (StringUtil.isEmpty(filePath)) { return null; } Map<String, String> map = null; FileInputStream fis = null; File f = new File(filePath); try { if (!f.exists() || !f.isFile()) { f.createNewFile(); } fis = new FileInputStream(f); Properties p = new Properties(); p.load(fis); map = new HashMap<String, String>((Map) p);// 因為properties繼承了map,所以直接通過p來構造一個map } catch (Exception e) { LogUtil.e(e); } finally { IOUtils.close(fis); } return map; } /** * 改名 */ public static boolean copy(String src, String des, boolean delete) { File file = new File(src); if (!file.exists()) { return false; } File desFile = new File(des); FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(file); out = new FileOutputStream(desFile); byte[] buffer = new byte[1024]; int count = -1; while ((count = in.read(buffer)) != -1) { out.write(buffer, 0, count); out.flush(); } } catch (Exception e) { LogUtil.e(e); return false; } finally { IOUtils.close(in); IOUtils.close(out); } if (delete) { file.delete(); } return true; } }