Android 文件管理器通用类 FileUtil
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.其他用法类似的。
    既然选择了,便不顾风雨兼程。Just follow yourself.
 
                    
                     
                    
                 
                    
                 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号