文件帮助类
来源:互联网 发布:农业普查数据 编辑:程序博客网 时间:2024/05/17 08:27
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();} elsedir.delete();}}
0 0
- 文件帮助类
- 文件帮助类
- C#文件帮助类FoderHelper
- C# 解压缩文件帮助类
- C# 文件下载帮助类
- C# 文件上传帮助类
- 资源文件帮助类ResourceHelper
- Android 文件操作帮助类FileUtils文件
- map 文件 帮助测试
- INF文件帮助文档
- 使用ILmerge合并Exe、Dll文件的帮助类
- java读取.properties文件及整合帮助类
- C# FTPClient--FTP操作帮助类,上传下载,文件,目录操作
- 基于Android 下载文件时,更新UI简单帮助类
- 基于Android 下载文件时,更新UI简单帮助类
- 又学一招——简单的写入文件帮助类
- FTP文件上传以及获取ftp配置帮助类
- 可能是史上最好用的 Java 文件操作帮助类
- 组队
- 5. Longest Palindromic Substring
- OpenGL深度缓存算法
- 159.View the Exhibit and examine the structure of the CUSTOMERS table.
- Qt中多线程的一种使用
- 文件帮助类
- 为自己立言!
- VBS实现文件逐字节异或
- view,SurfaceView,GLSurfaceView的关系和区别
- 复习最小生成树问题
- Android清除本地数据缓存代码
- linux
- java : 包装类 缓冲机制的使用
- [notes]chap14 Iterator Generator-expression 生成器表达式