Android SD卡,文件,文件夾工具

    /**
     * SD卡是否存在
     * 
     * @return true 存在 false 不存在
     */
    public boolean isSdExist() {
        boolean sdCardExist = Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED);
        return sdCardExist;
    }
    
    /**
     * 得到SD卡總大小
     * @parm sizeType: 返回的SD卡大小的單位 SIZETYPE_B,SIZETYPE_KB,SIZETYPE_MB,SIZETYPE_GB
     * @return double類型 SD卡大小
     */
    public double getSDTotalSize(int sizeType) {
        if (isSdExist()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long totalBlocks = stat.getBlockCount();
            return FormetFileSize(blockSize * totalBlocks, sizeType);
        }
        return 0;
    }
    /**
     * 得到SD卡總大小
     * @parm context  上下文
     * @return
     */
    public String getSDTotalSize(Context context) {
        if (isSdExist()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long totalBlocks = stat.getBlockCount();
            return Formatter.formatFileSize(context, blockSize * totalBlocks);
        }
        return "";
    }
    /**
     * 得到SD卡剩餘容量,便可用大小
     * 
     * @return
     */
    public double getSDAvailableSize(int sizeType) {
        if (isSdExist()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            return FormetFileSize(blockSize * availableBlocks, sizeType);
        }
        return 0;
    }
    /**
     * 得到SD卡剩餘容量,便可用大小
     * 
     * @return
     */
    public String getSDAvailableSize(Context context) {
        if (isSdExist()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            return Formatter.formatFileSize(context, blockSize * availableBlocks);
        }
        return "";
        
    }
    
    /**
     * 獲取指定文件大小
     * 
     * @param file
     * @return
     * @throws Exception
     */
    @SuppressWarnings("resource")
    public long getFileSize(File file) throws Exception {
        long size = 0;
        if (file.exists()) {
            FileInputStream fis = null;
            fis = new FileInputStream(file);
            size = fis.available();
        } else {
            file.createNewFile();
            Log.e("獲取文件大小", "文件不存在!");
        }
        return size;
    }
    
    /**
     * 獲取指定文件夾大小
     * 
     * @param f
     * @return
     * @throws Exception
     */
    public long getFileSizes(File f) throws Exception {
        long size = 0;
        File flist[] = f.listFiles();
        for (int i = 0; i < flist.length; i++) {
            if (flist[i].isDirectory()) {
                size = size + getFileSizes(flist[i]);
            } else {
                size = size + getFileSize(flist[i]);
            }
        }
        return size;
    }
    
    /**
     * 獲取指定文件的指定單位的大小
     * 
     * @param filePath 文件路徑
     * @param sizeType 獲取大小的類型1爲B、2爲KB、3爲MB、4爲GB
     * @return double值的大小
     */
    public double getFileOrFilesSize(String filePath, int sizeType) {
        File file = new File(filePath);
        if (!file.exists()) {
            return 0;
        }
        long blockSize = 0;
        try {
            if (file.isDirectory()) {
                blockSize = getFileSizes(file);
            } else {
                blockSize = getFileSize(file);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("獲取文件大小", "獲取失敗!");
        }
        return FormetFileSize(blockSize, sizeType);
    }
    
    public static final int SIZETYPE_B = 1;
    public static final int SIZETYPE_KB = 2;
    public static final int SIZETYPE_MB = 3;
    public static final int SIZETYPE_GB = 4;
    /**
     * 轉換文件大小,指定轉換的類型
     * 
     * @param fileS
     * @param sizeType
     * @return
     */
    private double FormetFileSize(long fileS, int sizeType) {
        DecimalFormat df = new DecimalFormat("#.00");
        double fileSizeLong = 0;
        switch (sizeType) {
        case SIZETYPE_B:
            fileSizeLong = Double.valueOf(df.format((double) fileS));
            break;
        case SIZETYPE_KB:
            fileSizeLong = Double.valueOf(df.format((double) fileS / 1024));
            break;
        case SIZETYPE_MB:
            fileSizeLong = Double.valueOf(df.format((double) fileS / 1048576));
            break;
        case SIZETYPE_GB:
            fileSizeLong = Double.valueOf(df
                    .format((double) fileS / 1073741824));
            break;
        default:
            break;
        }
        return fileSizeLong;
    }
    
    /**
     * 建立文件
     * 
     * @param path 文件夾路徑
     * @param fileName 文件名稱
     * @return
     */
    public File getOutFile(String path, String fileName) {
        if (!isSdExist()) {
            return null;
        }
        if (path != null) {
            File mediaStorageDir = new File(path);
            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                    return null;
                }
            }
        }
        File f = new File(path + fileName);
        if (!f.exists()) {
            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } 
        return f;
    }
    
    /**
     * 向已建立的文件中寫入數據
     * @param str 寫入內容
     * @param fileName 文件名稱
     */ 
    @SuppressLint("SimpleDateFormat")
    public void print(String str, String fileName) {

        // 獲取SD卡剩餘大小
        double sdSize = getSDAvailableSize();
        if (sdSize < 3) {
            return;
        }
        FileWriter fw = null;
        BufferedWriter bw = null;
        String datetime = "";
        try {
            SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd" + " " + "hh:mm:ss");
            datetime = tempDate.format(new java.util.Date()).toString();//插入日期時間
            fw = new FileWriter(filenameTemp, true);//
            // 建立FileWriter對象,用來寫入字符流
            bw = new BufferedWriter(fw); // 將緩衝對文件的輸出
            String myreadline = "[ " + datetime + "]" + str + "\n";
            bw.write(myreadline); // 寫入文件
            bw.newLine();
            bw.flush(); // 刷新該流的緩衝
            bw.close();
            fw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            try {
                bw.close();
                fw.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
            }
        }
    }
    
    /** 
     * 得到機身內存總大小 
     *  
     * @return 
     */  
    public String getRomTotalSize(Context context) {  
        File path = Environment.getDataDirectory();  
        StatFs stat = new StatFs(path.getPath());  
        long blockSize = stat.getBlockSize();  
        long totalBlocks = stat.getBlockCount();  
        return Formatter.formatFileSize(context, blockSize * totalBlocks);
    }
    
    /** 
     * 得到機身內存總大小 
     * @parm sizeType 返回大小的單位
     * @return 
     */  
    public double getRomTotalSize(int sizeType) {  
        File path = Environment.getDataDirectory();  
        StatFs stat = new StatFs(path.getPath());  
        long blockSize = stat.getBlockSize();  
        long totalBlocks = stat.getBlockCount();
        return FormetFileSize(blockSize * totalBlocks , sizeType);
    }
    
    /** 
     * 得到機身可用內存 
     *  
     * @return 
     */  
    private String getRomAvailableSize(Context context) {  
        File path = Environment.getDataDirectory();  
        StatFs stat = new StatFs(path.getPath());  
        long blockSize = stat.getBlockSize();  
        long availableBlocks = stat.getAvailableBlocks();  
        return Formatter.formatFileSize(context, blockSize * availableBlocks);  
    } 
    
    /** 
     * 得到機身可用內存 
     *  
     * @return 
     */  
    private double getRomAvailableSize(inte sizeType) {  
        File path = Environment.getDataDirectory();  
        StatFs stat = new StatFs(path.getPath());  
        long blockSize = stat.getBlockSize();  
        long availableBlocks = stat.getAvailableBlocks();  
        return FormetFileSize(blockSize * availableBlocks , sizeType);  
    }
相關文章
相關標籤/搜索