標籤:
自己寫的工具類,寫的不好,慢慢修改。
記得加上許可權
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
package com.sy.utils;import android.content.Context;import android.os.Environment;import android.os.StatFs;import android.util.Log;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;/** * Created by SY on 2016/5/9. * 檔案讀寫工具類 */public class FileUtils { private Context context; private String SDCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();//sd卡路徑 private static String SDState = Environment.getExternalStorageState();//SD卡狀態 public FileUtils(Context context) { this.context = context; } /** * 檔案儲存體到/data/data/<packagename>/files/預設目錄下 * * @param fileName * @param bytes * @return */ public boolean write2CacheFile(String fileName, byte[] bytes) { FileOutputStream out = null; BufferedOutputStream bos = null; try { out = context.openFileOutput(fileName, Context.MODE_PRIVATE); bos = new BufferedOutputStream(out); bos.write(bytes); bos.flush(); return true; } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; } /** * 向SD卡裡寫位元組 * * @param path 檔案夾目錄 * @param file 檔案名稱 * @param data 寫入的位元組數組 * @return */ public static boolean writeBytes(String path, String file, byte[] data) { FileOutputStream fos = null; try { // 擁有足夠的容量 if (data.length < getSDFreeSize()) { createDirectoryIfNotExist(path); createFileIfNotExist(path + file); fos = new FileOutputStream(path + File.separator + file); fos.write(data); fos.flush(); return true; } }catch (Exception e){ Log.e("writeBytes", e.getMessage()); }finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } return false; } } /** * 從SD卡裡讀取位元組數組 * * @param path 目錄 * @param fileName 檔案名稱 * @return 返回位元組數組,檔案不存在返回null */ public static byte[] readBytes(String path, String fileName) { File file = new File(path + File.separator + fileName); if (!file.exists()) { return null; } InputStream inputStream = null; try { inputStream = new BufferedInputStream(new FileInputStream(file)); byte[] data = new byte[inputStream.available()]; inputStream.read(data); return data; } catch (IOException e) { e.printStackTrace(); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; } /** * 將一個位元組流寫入到SD卡檔案 * * @param path 目錄路徑 * @param fileName 檔案名稱 * @param input 位元組流 * @return */ public static Boolean write2SDFromInput(String path, String fileName, InputStream input) { File file = null; OutputStream output = null; try { int size = input.available(); // 擁有足夠的容量 if (size < getSDFreeSize()) { createDirectoryIfNotExist(path); createFileIfNotExist(path + File.separator + fileName); file = new File(path + File.separator + fileName); output = new BufferedOutputStream(new FileOutputStream(file)); byte buffer[] = new byte[1024]; int temp; while ((temp = input.read(buffer)) != -1) { output.write(buffer, 0, temp); } output.flush(); return true; } } catch (IOException e1) { e1.printStackTrace(); } finally { try { if (output != null) { output.close(); } } catch (Exception e) { e.printStackTrace(); } } return false; } /** * 判斷SD卡是否存在 * * @ return */ private static boolean SDCardisExist() { if (SDState.equals(Environment.MEDIA_MOUNTED)) { return true; } else return false; } /** * 擷取SD卡剩餘容量大小(單位Byte) * * @return */ public static long getSDFreeSize() { //取得SD卡檔案路徑 File path = Environment.getExternalStorageDirectory(); StatFs sf = new StatFs(path.getPath()); //擷取單個資料區塊的大小(Byte) long blockSize = sf.getBlockSize(); //閒置資料區塊的數量 long freeBlocks = sf.getAvailableBlocks(); //返回SD卡空閑大小 return freeBlocks * blockSize; //單位Byte //return (freeBlocks * blockSize)/1024; //單位KB// return (freeBlocks * blockSize) / 1024 / 1024; //單位MB } /** * 擷取SD卡總容量大小(單位Byte) * * @return */ public static long getSDAllSize() { //取得SD卡檔案路徑 File path = Environment.getExternalStorageDirectory(); StatFs sf = new StatFs(path.getPath()); //擷取單個資料區塊的大小(Byte) long blockSize = sf.getBlockSize(); //擷取所有資料區塊數 long allBlocks = sf.getBlockCount(); //返回SD卡大小 return allBlocks * blockSize; //單位Byte //return (allBlocks * blockSize)/1024; //單位KB// return (allBlocks * blockSize) / 1024 / 1024; //單位MB } /** * 如果目錄不存在,就建立目錄 * * @param path 目錄 * @return */ public static boolean createDirectoryIfNotExist(String path) { File file = new File(path); //如果檔案夾不存在則建立 if (!file.exists() && !file.isDirectory()) { return file.mkdirs(); } else { Log.e("目錄", "目錄存在!"); return false; } } /** * 如果檔案不存在,就建立檔案 * @param path 檔案路徑 * @return */ public static boolean createFileIfNotExist(String path) { File file = new File(path); try { if (!file.exists()) { return file.createNewFile(); } else { Log.e("檔案", "檔案存在!"); return false; } } catch (Exception e) { Log.e("error", e.getMessage()); return false; } }}
Android 檔案讀寫工具類