即拿即用一Android文件存取

September 15, 2017

写文件

/**
 * 写文件
 *
 * @param filePath 文件绝对路径
 * @param content  写入内容
 * @return 写入是否成功
 */
public static boolean writeFile(String filePath, String content) {
    FileOutputStream fileOutputStream = null;
    BufferedOutputStream bufferedOutputStream = null;
    try {
        File file = new File(filePath);
        if (file.createNewFile()) {
            fileOutputStream = new FileOutputStream(filePath);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            bufferedOutputStream.write(content.getBytes("UTF-8"));
            bufferedOutputStream.flush();
            return true;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (null != fileOutputStream) {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (null != bufferedOutputStream) {
            try {
                bufferedOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

读文件

/**
 * 读文件
 *
 * @param filePath 文件路径
 * @return 文件内容
 */
public static String readFile(String filePath) {

    StringBuilder stringBuffer = new StringBuilder();

    File file = new File(filePath);

    if (file.exists()) {
        FileInputStream fileInputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {
            fileInputStream = new FileInputStream(file);
            inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            String mimeTypeLine;
            while ((mimeTypeLine = bufferedReader.readLine()) != null) {
                stringBuffer.append(mimeTypeLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fileInputStream) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != inputStreamReader) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != bufferedReader) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    return stringBuffer.toString();
}

Assets文件读取

/**
 * 读取 Assets 文件
 *
 * @param context  context
 * @param filePath 文件路径
 * @return 文件内容
 */
public static String readAssetsFile(Context context, String filePath) {
    StringBuilder stringBuilder = new StringBuilder();

    InputStream inputStream = null;
    InputStreamReader inputStreamReader = null;
    BufferedReader bufferedReader = null;
    try {
        inputStream = context.getAssets().open(filePath);
        inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        bufferedReader = new BufferedReader(inputStreamReader);

        String mimeTypeLine;
        while ((mimeTypeLine = bufferedReader.readLine()) != null) {
            stringBuilder.append(mimeTypeLine)/*.append("\n")*/;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (null != inputStream) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (null != inputStreamReader) {
            try {
                inputStreamReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (null != bufferedReader) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return stringBuilder.toString();
}

Bitmap保存

/**
 * 保存Bitmap到本地
 *
 * @param bitmap   Bitmap
 * @param dir      保存路径
 * @param fileName 保存的文件名
 * @return 保存是否成功
 */
public static boolean saveBitmap(Bitmap bitmap, String dir, String fileName) {
    FileOutputStream fileOutputStream = null;
    try {
        File fileDir = new File(dir);
        if (fileDir.exists() || fileDir.mkdirs()) {
            // 文件存在
            File file = new File(dir + "/" + fileName);
            if (file.createNewFile()) {
                fileOutputStream = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
                fileOutputStream.flush();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        if (null != fileOutputStream) {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return true;
}

文件删除

/**
 * 删除文件
 *
 * @param filePath 文件路径
 * @return 删除是否成功
 */
public static boolean deleteFile(String filePath) {
    File file = new File(filePath);
    return file.exists() && file.delete();
}