即拿即用一Android文件存取 发表于 2017-09-15 | 写文件 读文件 Assets文件读取 Bitmap保存 文件删除 写文件123456789101112131415161718192021222324252627282930313233343536373839/** * 写文件 * * @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;} 读文件123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354/** * 读文件 * * @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文件读取12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849/** * 读取 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保存1234567891011121314151617181920212223242526272829303132333435/** * 保存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;} 文件删除12345678910/** * 删除文件 * * @param filePath 文件路径 * @return 删除是否成功 */public static boolean deleteFile(String filePath) { File file = new File(filePath); return file.exists() && file.delete();} 坚持原创技术分享,您的支持将鼓励我继续创作! 赏 微信打赏 支付宝打赏