一个文件工具类

来源:互联网 发布:主机mac地址怎么查 编辑:程序博客网 时间:2024/06/05 11:17
package com.lx.xxx.util;


import java.io.File;


import android.os.Environment;
/**
* 文件工具类
*/
public class FileUtils {
public static final String CACHE = "cache";
public static final String ICON = "icon";
public static final String ROOT = "qqqqqq";


/**
* 获取图片的缓存的路径

* @return  路径   
*/
public static File getIconDir() {
return getDir(ICON);


}


/**
* 获取缓存路径

* @return  缓存路径
*/
public static File getCacheDir() {
return getDir(CACHE);
}  
public static File getDir(String cache) {
StringBuilder path = new StringBuilder();
if (isSDAvailable()) {
//内存卡中
path.append(Environment.getExternalStorageDirectory()//   /mnt/sdcard
.getAbsolutePath());
path.append(File.separator);// '/'
path.append(ROOT);// /mnt/sdcard/qqqqqq
path.append(File.separator);
path.append(cache);// /mnt/sdcard/qqqqqq/cache
} else {
//应用的文件中
File filesDir = UiUtils.getContext().getCacheDir(); //getCacheDir()保存临时数据 cache
// getFileDir()保存永久数据  file

path.append(filesDir.getAbsolutePath());//getAbsolutePath()绝对路径     /data/data/包名
path.append(File.separator);// /data/data/com.lx.xxx/
path.append(cache);// /data/data/包名/cache
}
File file = new File(path.toString());
if (!file.exists() || !file.isDirectory()) {
file.mkdirs();// 创建文件夹
}
return file;


}
      /**
       * 是否存在sd卡
       * @return   存在true
       */
private static boolean isSDAvailable() {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}


}
0 0