文件帮助类


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();
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值