Android文件操作(SD卡、缓存)工具类

getCacheDir()方法用于获取/data/data/<application package>/cache目录
getFilesDir()方法用于获取/data/data/<application package>/files目录
应用程序在运行的过程中如果需要向手机上保存数据,一般是把数据保存在SDcard中的。
大部分应用是直接在SDCard的根目录下创建一个文件夹,然后把数据保存在该文件夹中。
这样当该应用被卸载后,这些数据还保留在SDCard中,留下了垃圾数据。
如果你想让你的应用被卸载后,与该应用相关的数据也清除掉,该怎么办呢?
通过Context.getExternalFilesDir()方法可以获取到 SDCard/Android/data/你的应用的包名/files/ 目录,
一般放一些长时间保存的数据
通过Context.getExternalCacheDir()方法可以获取到 SDCard/Android/data/你的应用包名/cache/目录,
一般存放临时缓存数据
如果使用上面的方法,当你的应用在被用户卸载后,SDCard/Android/data/你的应用的包名/ 这个目录下的所有文件都会被删除,
不会留下垃圾信息。
而且上面二个目录分别对应 设置->应用->应用详情里面的”清除数据“与”清除缓存“选项

如果要保存下载的内容,就不要放在以上目录下


public class FileUtils {
    public static String getDataFolderPath(Context paramContext) {
        return Environment.getDataDirectory() + "/data/"  + paramContext.getPackageName() + "/files";
    }

    public static String getMyFileDir(Context context){
        return context.getFilesDir().toString();
    }

    public static String getMyCacheDir(Context context){
        return context.getCacheDir().toString();
    }
    /**  * @desc 保存内容到文件中  * @param fileName  * @param content  * @throws Exception  */  public static void save(Context context, String fileName, String content, int module) {
        try {
            FileOutputStream os = context.openFileOutput(fileName, module);
            os.write(content.getBytes());
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**  * @desc 读取文件内容  * @param fileName  * @return  */  public static String read(Context context, String fileName){

        try {
            FileInputStream fis = context.openFileInput(fileName);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int len = 0;
            while((len = fis.read(b)) != -1){
                bos.write(b, 0, len);
            }
            byte[] data = bos.toByteArray();
            fis.close();
            bos.close();
            return new String(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**  * @desc 将文本内容保存到sd卡的文件中  * @param context  * @param fileName  * @param content  * @throws IOException  */  public static void saveToSDCard(Context context, String fileName, String content) throws IOException {

        File file = new File(Environment.getExternalStorageDirectory(),fileName);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(content.getBytes());
        fos.close();
    }

    /**  * @desc 读取sd卡文件内容  * @param fileName  * @return  * @throws IOException  */  public static String readSDCard(String fileName) throws IOException {

        File file = new File(Environment.getExternalStorageDirectory(),fileName);
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buffer =  new byte[1024];
        int len = 0;
        while((len = fis.read(buffer)) != -1)
        {
            bos.write(buffer, 0, len);
        }
        byte[]  data = bos.toByteArray();
        fis.close();
        bos.close();

        return new String(data);
    }
    /*获取缓存路径,存储临时文件,可被一键清理和卸载清理*/  /*  * 可以看到,当SD卡存在或者SD卡不可被移除的时候,  * 就调用getExternalCacheDir()方法来获取缓存路径,  * 否则就调用getCacheDir()方法来获取缓存路径。  * 前者获取到的就是/sdcard/Android/data/<application package>/cache 这个路径,  * 而后者获取到的是 /data/data/<application package>/cache 这个路径。*/  public static File getDiskCacheDir(Context context, String uniqueName) {
        String cachePath;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            cachePath = context.getExternalCacheDir().getPath();
        } else {
            cachePath = context.getCacheDir().getPath();
        }
        return new File(cachePath + File.separator + uniqueName);
    }
    /*返回缓存路径*/  public static File getDiskCacheDir(Context context) {
        String cachePath;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            cachePath = context.getExternalCacheDir().getPath();
        } else {
            cachePath = context.getCacheDir().getPath();
        }
        return new File(cachePath);
    }
    /*判断sd卡可用*/  public static boolean hasSDCardMounted() {
        String state = Environment.getExternalStorageState();
        if (state != null && state.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

你可能感兴趣的:(Android文件操作(SD卡、缓存)工具类)