开发者博客:[url]http://www.developsearch.com[/url]
/**
* 文件工具类
*
* @author chenxin
* @version [版本号, 2012-5-21]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class FileUtil {
/**
* 删除一个文件夹下的所有文件
*
* @param folderPath
*/
public void delAllFiles(String folderPath){
File f = new File(folderPath);//定义文件路径
if(f.exists() && f.isDirectory()){//判断是文件还是目录
if(f.listFiles().length==0){
//f.delete();//若目录下没有文件则直接删除
}else{//若有则把文件放进数组,并判断是否有下级目录
File delFile[]=f.listFiles();
for(int j=0;j<f.listFiles().length;j++){
//if(delFile[j].isDirectory()){//判断是否有下级目录
//递归调用del方法并取得子目录路径
//delAllFiles(delFile[j].getAbsolutePath());
//}
delFile[j].delete();//删除文件
}
}
}
}
/**
* 检查文件夹是否存在,不存在就创建一个
*
* @param folderPath
*/
public void folderCheck(String folderPath){
File f = new File(folderPath);
if(f.exists() && f.isDirectory()){
//do nothing
}else{
f.mkdir();
}
}
/**
* 根据文件的路径获取文件名称
* @param filePath 文件路径
* @return
* @see [类、类#方法、类#成员]
*/
public static String getFileName(String filePath)
{
filePath = filePath.replace('\\', '/');
int pos = filePath.lastIndexOf("/");
return filePath.substring(pos + 1);
}
/**
* 得到文件扩展名
* @param fileName
* @return
*/
public static String getFileExt(String fileName){
if(Empty.isEmpty(fileName)){
return "";
}
return fileName.substring(fileName.lastIndexOf('.'),fileName.length());
}
}