1.整體分析
1.1.源代碼如下,可以直接Copy。

public class FileUtil { private FileUtil() { } //****系統文件目錄********************************************************************************************** /** * @return 程序系統文件目錄 */ public static String getFileDir(Context context) { return String.valueOf(context.getFilesDir()); } /** * @param context 上下文 * @param customPath 自定義路徑 * @return 程序系統文件目錄絕對路徑 */ public static String getFileDir(Context context, String customPath) { String path = context.getFilesDir() + formatPath(customPath); mkdir(path); return path; } //****系統緩存目錄********************************************************************************************** /** * @return 程序系統緩存目錄 */ public static String getCacheDir(Context context) { return String.valueOf(context.getCacheDir()); } /** * @param context 上下文 * @param customPath 自定義路徑 * @return 程序系統緩存目錄 */ public static String getCacheDir(Context context, String customPath) { String path = context.getCacheDir() + formatPath(customPath); mkdir(path); return path; } //****Sdcard文件目錄********************************************************************************************** /** * @return 內存卡文件目錄 */ public static String getExternalFileDir(Context context) { return String.valueOf(context.getExternalFilesDir("")); } /** * @param context 上下文 * @param customPath 自定義路徑 * @return 內存卡文件目錄 */ public static String getExternalFileDir(Context context, String customPath) { String path = context.getExternalFilesDir("") + formatPath(customPath); mkdir(path); return path; } //****Sdcard緩存目錄********************************************************************************************** /** * @return 內存卡緩存目錄 */ public static String getExternalCacheDir(Context context) { return String.valueOf(context.getExternalCacheDir()); } /** * @param context 上下文 * @param customPath 自定義路徑 * @return 內存卡緩存目錄 */ public static String getExternalCacheDir(Context context, String customPath) { String path = context.getExternalCacheDir() + formatPath(customPath); mkdir(path); return path; } //****公共文件夾********************************************************************************************** /** * @return 公共下載文件夾 */ public static String getPublicDownloadDir() { return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(); } //****相關工具********************************************************************************************** /** * 創建文件夾 * * @param DirPath 文件夾路徑 */ public static void mkdir(String DirPath) { File file = new File(DirPath); if (!(file.exists() && file.isDirectory())) { file.mkdirs(); } } /** * 格式化文件路徑 * 示例: 傳入 "sloop" "/sloop" "sloop/" "/sloop/" * 返回 "/sloop" */ private static String formatPath(String path) { if (!path.startsWith("/")) path = "/" + path; while (path.endsWith("/")) path = new String(path.toCharArray(), 0, path.length() - 1); return path; } /** * @return 存儲卡是否掛載(存在) */ public static boolean isMountSdcard() { String status = Environment.getExternalStorageState(); return status.equals(Environment.MEDIA_MOUNTED); } }
1.2.主要方法。
- 獲取系統文件目錄
- 獲取系統文件目錄+新建自定義路徑
- 獲取系統緩存目錄
- 獲取系統緩存目錄+新建自定義路徑
- 獲取外部存儲文件目錄
- 獲取外部存儲文件目錄+新建自定義路徑
- 獲取外部存儲緩存文件目錄
- 獲取外部存儲緩存文件目錄+新建自定義路徑
- 獲取公共下載文件夾
- 創建文件夾
- 格式化文件路徑
- 判斷外部存儲是否存在
2.局部分析
2.1.構造函數
空的構造函數。
2.2.系統文件目錄
這里系統文件目錄,調用了context.getFileDir()直接獲取到。
下面有一個formatPath(傳一個自定義參數)
傳入一個字符串,然后轉化為正確格式即可。
然后是新建文件夾mkdir(一個路徑參數)
判斷文件如果不存在,且文件不是一個路徑,然后在創建文件夾。
2.3.獲取系統緩存目錄
方法基本一致,調用context.getCacheDir()獲取。
2.4.獲取外部存儲文件目錄
調用了context.getExternalFilesDir("")即可獲取。
2.5.獲取外部存儲緩存目錄。
同樣調用了context.getExternalCacheDir()方法獲取。
2.6.獲取公共下載文件
直接獲取系統的下載文件夾。
2.7.判斷是否存在外部存儲(現在手機基本都有)
3.簡單用法
3.1.用法很簡單
例如有個要顯示緩存大小
這里用到了這個通用類來獲取外部存儲器的緩存地址。
3.2.其他用法類似的。