android开发SDcard 响应的文件相关处理(一)

android开发相关文件类的处理工具类:

  1 package com.gzcivil.utils;
  2 
  3 import java.io.File;
  4 import java.util.ArrayList;
  5 import java.util.List;
  6 
  7 import android.os.Environment;
  8 import android.os.StatFs;
  9 
 10 import com.gzcivil.entity.FileEntity;
 11 
 12 /**
 13  * SDcard 相关处理
 14  * 
 15  * @author LiJinlun
 16  * 
 17  */
 18 public class FileUtils {
 19 
 20     /**
 21      * 获取指定文件夹  所有文件
 22      * @param Path
 23      * @param Extension
 24      * @param IsIterative
 25      * @return
 26      */
 27     public static List<FileEntity> GetFiles(String Path, String Extension, boolean IsIterative) // 搜索目录,扩展名(判断的文件类型的后缀名),是否进入子文件夹
 28     {
 29         List<FileEntity> mlist = new ArrayList<FileEntity>();
 30         if (mlist != null && mlist.size() > 0)
 31             mlist.clear();
 32         File[] files = new File(Path).listFiles();
 33         if (files != null) {
 34             for (int i = 0; i < files.length; i++) {
 35                 File f = files[i];
 36                 if (f.isFile()) {
 37                     if (f.getPath().substring(f.getPath().length() - Extension.length()).equals(Extension)) // 判断扩展名
 38                     {
 39                         FileEntity fileEnt = new FileEntity();
 40                         fileEnt.setNewName(f.getName());
 41                         fileEnt.setFileSize(CommonUtil.FormetFileSize(f.length()));
 42                         fileEnt.setFilePath(f.getPath());
 43 
 44                         mlist.add(fileEnt);
 45                     }
 46                     if (!IsIterative)
 47                         break; // 如果不进入子集目录则跳出
 48                 } else if (f.isDirectory() && f.getPath().indexOf("/.") == -1) // 忽略点文件(隐藏文件/文件夹)
 49                     GetFiles(f.getPath(), Extension, IsIterative); // 这里就开始递归了
 50             }
 51 
 52         }
 53         return mlist;
 54     }
 55 
 56     /**
 57      * sdcard 是否存在
 58      * 
 59      * @return
 60      */
 61     public static boolean ExistSDCard() {
 62         if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
 63             return true;
 64         } else
 65             return false;
 66     }
 67 
 68     /**
 69      * sdcard 剩余空间
 70      * 
 71      * @return
 72      */
 73     public static long getSDFreeSize() {
 74         // 取得SD卡文件路径
 75         File path = Environment.getExternalStorageDirectory();
 76         StatFs sf = new StatFs(path.getPath());
 77         // 获取单个数据块的大小(Byte)
 78         @SuppressWarnings("deprecation")
 79         long blockSize = sf.getBlockSize();
 80         // 空闲的数据块的数量
 81         @SuppressWarnings("deprecation")
 82         long freeBlocks = sf.getAvailableBlocks();
 83         // 返回SD卡空闲大小
 84         // return freeBlocks * blockSize; //单位Byte
 85         // return (freeBlocks * blockSize)/1024; //单位KB
 86         return (freeBlocks * blockSize) / 1024 / 1024; // 单位MB
 87     }
 88 
 89     /**
 90      * sdcard 总容量
 91      * 
 92      * @return
 93      */
 94     public static long getSDAllSize() {
 95         // 取得SD卡文件路径
 96         File path = Environment.getExternalStorageDirectory();
 97         StatFs sf = new StatFs(path.getPath());
 98         // 获取单个数据块的大小(Byte)
 99         @SuppressWarnings("deprecation")
100         long blockSize = sf.getBlockSize();
101         // 获取所有数据块数
102         @SuppressWarnings("deprecation")
103         long allBlocks = sf.getBlockCount();
104         // 返回SD卡大小
105         // return allBlocks * blockSize; //单位Byte
106         // return (allBlocks * blockSize)/1024; //单位KB
107         return (allBlocks * blockSize) / 1024 / 1024; // 单位MB
108     }
109 
110     // 将SD卡文件夹里文件删除
111     public static void deleteFile(File file) {
112         if (file.exists()) {
113             if (file.isFile()) {
114                 return;
115             } else if (file.isDirectory()) {
116                 // 如果它是一个目录
117                 // 声明目录下所有的文件 files[];
118                 File files[] = file.listFiles();
119                 for (int i = 0; i < files.length; i++) { // 遍历目录下所有的文件
120                     deleteFile(files[i]); // 把每个文件 用这个方法进行迭代
121                 }
122             }
123             file.delete();
124         }
125     }
126 
127     /**
128      * 删除单个文件
129      * 
130      * @param file
131      */
132     public static void deleteSingleFile(File file) {
133         if (file.exists()) {
134             if (file.isFile()) {
135                 file.delete();
136             }
137         }
138     }
139 }
原文地址:https://www.cnblogs.com/lijinlun0825/p/5174692.html