package com.noprom.app.common;
import android.content.Context;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* 文件操作工具包
*
* @author noprom (http://github.com/noprom)
* @version 1.0
* Created by noprom on 2015/2/22.
*/
public class FileUtils {
/**
* 清空一个文件夹
*/
public static void clearFileWithPath(String filePath) {
List<File> files = FileUtils.listPathFiles(filePath);
if (files.isEmpty()) {
return;
}
for (File f : files) {
if (f.isDirectory()) {
clearFileWithPath(f.getAbsolutePath());
} else {
f.delete();
}
}
}
/**
* 列出root目录下面的所有子目录
*
* @param root
* @return
*/
public static List<String> listPath(String root) {
List<String> allDir = new ArrayList<String>();
SecurityManager checker = new SecurityManager();
File path = new File(root);
checker.checkRead(root);
// 过滤掉以.开始的文件夹
if (path.isDirectory()) {
for (File f : path.listFiles()) {
if (f.isDirectory() && !f.getName().startsWith(".")) {
allDir.add(f.getAbsolutePath());
}
}
}
return allDir;
}
/**
* 获取一个文件夹下面的所有文件
*
* @param root
* @return
*/
public static List<File> listPathFiles(String root) {
List<File> allDir = new ArrayList<File>();
SecurityManager checker = new SecurityManager();
File path = new File(root);
checker.checkRead(root);
File[] files = path.listFiles();
for (File f : files) {
if (f.isFile())
allDir.add(f);
else listPath(f.getAbsolutePath());
}
return allDir;
}
/**
* 获取应用程序缓存文件夹下面的指定目录
*
* @param context
* @param dir
* @return
*/
public static String getAppCache(Context context, String dir) {
String savePath = context.getCacheDir().getAbsolutePath() + '/' + dir + '/';
File saveDir = new File(savePath);
if (!saveDir.exists()) {
saveDir.mkdirs();
}
saveDir = null;
return savePath;
}
/**
* 根据文件绝对路径获取文件名
*
* @param filePath
* @return
*/
public static String getFileName(String filePath) {
if (StringUtils.isEmpty(filePath))
return "";
return filePath.substring(filePath.lastIndexOf(File.separator) + 1);
}
}