文件帮助类
2016-03-15 20:10
363 查看
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Context; import android.os.Environment; import android.os.StatFs; import android.text.TextUtils; /** * 文件帮助类 * * */ public class FileHelper { /** * SD卡是否存在 * * @return */ public static boolean isSDCardExist() { String SDCardState = Environment.getExternalStorageState(); return Environment.MEDIA_MOUNTED.equals(SDCardState); } /** * 文件或文件夹是否存在 * * @param filename * @return */ public static boolean isExist(String path) { if (TextUtils.isEmpty(path)) { return false; } File file = new File(path); return file.exists(); } /** * 创建文件夹 * * @param folderPath * 文件夹路径 */ public static boolean createFolder(String folderPath) { if (TextUtils.isEmpty(folderPath)) { return false; } File folder = new File(folderPath); if (folder.exists()) { if (isFolderReadable(folderPath)) { // 路径存在并可以读取,不用重新创建 return true; } // 路径存在但是无法读取,删除路径 folder.delete(); } return folder.mkdirs(); } /** * 文件夹是否可以读取 * * @param file * @return */ private static boolean isFolderReadable(String folderPath) { File tempFile = new File(folderPath + "temp.txt"); FileOutputStream tempStream = null; try { tempStream = new FileOutputStream(tempFile); return true; } catch (Exception e) { return false; } finally { try { tempStream.close(); } catch (Exception e) { } try { tempFile.delete(); } catch (Exception e) { } } } /** * 在指定路径下创建文件 若路径不存在,先创建路径 * * @param path * 文件路径 * @param fileName * 文件名 * @return 文件 */ public static File createFile(String path, String fileName) { if (TextUtils.isEmpty(path) || TextUtils.isEmpty(fileName)) { return null; } if (!createFolder(path)) { return null; } /** * separator与系统有关的默认名称分隔符,在UNIX系统上,此字符为“/”,在windows系统上,为“\” */ File file = new File(path + File.separator + fileName); try { file.createNewFile(); return file; } catch (IOException e) { return null; } } /** * 拷贝一个文件 srcFile源文件 destFile目标文件 * * @param srcFileString * @param destFileString * @return * @throws IOException */ public static boolean copyFileTo(String srcFileString, String destFileString) throws IOException { if (TextUtils.isEmpty(srcFileString) || TextUtils.isEmpty(destFileString)) { return false; } File srcFile = new File(srcFileString); File destFile = new File(destFileString); if (srcFile.isDirectory() || destFile.isDirectory()) { /** * File.isDirectory如果path表示的是一个目录就返回true 如果是一个文件就返回false */ return false;// 判断是否是文件 } FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); int readLen = 0; byte[] buf = new byte[1024]; while ((readLen = fis.read(buf)) != -1) { fos.write(buf, 0, readLen); } fos.flush(); fos.close(); fos = null; fis.close(); fis = null; return true; } /** * 获取手机内存可用空间 */ @SuppressWarnings("deprecation") public static long getFreeSpace(Context context) { if (context == null) { return 0; } File filesDir = context.getFilesDir(); if (filesDir == null) { return 0; } /** * StatFs类主要用来获取文件系统的状态,能够获取sd卡的大小和剩余空间,获取系统内部空间(system的大小)和剩余空间等 */ StatFs statfs = new StatFs(filesDir.getPath()); return (long) (statfs.getBlockSize()) * statfs.getAvailableBlocks(); } /** * 删除文件 * * @param fileFullPath */ public boolean deleteFile(String filePath) { if (TextUtils.isEmpty(filePath)) { return false; } File file = new File(filePath); if (file == null || !file.exists() || file.isDirectory()) { return false; } return file.delete(); } /** * 清空目录下所有文件和文件夹 */ public static void deleteDir(String rootPath) { if (TextUtils.isEmpty(rootPath)) { return; } File file = new File(rootPath); if (file == null || !file.exists()) { return; } File[] files = file.listFiles(); if (files != null && files.length > 0) { for (int i = 0; i < files.length; i++) { deleteFolderIncludeSelf(files[i]); } } } /** * 删除文件和文件夹 * * @param dir */ private static void deleteFolderIncludeSelf(File dir) { if (dir == null || !dir.exists()) return; if (dir.isDirectory()) { File[] files = dir.listFiles(); for (File file : files) if (file.isDirectory()) { deleteFolderIncludeSelf(file); } else { file.delete(); } dir.delete(); } else dir.delete(); } }
相关文章推荐
- 第三周项目2
- C#基础之反射
- Android AsyncTask分析
- 【洛谷1282】多米诺骨牌
- C++之类与对象(1)
- BZOJ3925: [Zjoi2015]地震后的幻想乡
- linux
- 组队
- Android清除本地数据缓存代码
- nginx window安装
- VBS实现文件逐字节异或
- 12306 2.2版本SO的分析和修复
- 复习最小生成树问题
- view,SurfaceView,GLSurfaceView的关系和区别
- java : 包装类 缓冲机制的使用
- python 之socket语法及相关
- 【乱码】乱码整理
- [BZOJ 3998]弦论
- iOS平台的数据库框架 FMDB
- memcheck c/c++内存泄漏检测