/** FileUtil.java
* Copyright (C) 2007-3-19
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
**/packageorg.lambdasoft.utils;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.FileReader;importjava.io.InputStream;importjava.util.Date;importjava.util.HashMap;importjava.util.Map;importjava.util.Properties;importjava.util.Random;importjava.util.Set;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;/*** 文件工具类
*
*@authorTangLei
* @date 2009-2-24*/publicclassFileUtil {privatestaticLog log=LogFactory.getLog(FileUtil.class);privateFileUtil() {}/*** 获取随机的文件名称
*@paramseed 随机种子
*@return*/publicstaticString getRandomFileName(String seed) {byte[] ra=newbyte[100];newRandom().nextBytes(ra);
StringBuilder build=newStringBuilder("");for(inti=0; i
build.append(Byte.valueOf(ra[i]).toString());
}
String currentDate=Long.valueOf(newDate().getTime()).toString();
seed=seed+currentDate+build.toString();returnEncryptUtils.getMD5ofStr(seed).toLowerCase();
}/*** 列出所有当前层的文件和目录
*
*@paramdir 目录名称
*@returnfileList 列出的文件和目录*/publicstaticFile[] ls(String dir) {returnnewFile(dir).listFiles();
}/*** 根据需要创建文件夹
*
*@paramdirPath 文件夹路径
*@paramdel 存在文件夹是否删除*/publicstaticvoidmkdir(String dirPath,booleandel) {
File dir=newFile(dirPath);if(dir.exists()) {if(del)
dir.delete();elsereturn;
}
dir.mkdirs();
}/*** 删除文件和目录
*
*@parampath
*@throwsException*/publicstaticvoidrm(String path)throwsException{if(log.isDebugEnabled())
log.debug("需要删除的文件:"+path);
File file=newFile(path);if(!file.exists()) {if(log.isWarnEnabled())
log.warn("文件不存在");return;
}if(file.isDirectory()) {
File[] fileList=file.listFiles();if(fileList==null||fileList.length==0) {
file.delete();
}else{for(File _file : fileList) {
rm(_file.getAbsolutePath());
}
}
file.delete();
}else{
file.delete();
}
}/*** 移动文件
*
*@paramsource 源文件
*@paramtarget 目标文件
*@paramcache 文件缓存大小
*@throwsException*/publicstaticvoidmv(String source,String target,intcache)throwsException {if(source.trim().equals(target.trim()))return;byte[] cached=newbyte[cache];
FileInputStream fromFile=newFileInputStream(source);
FileOutputStream toFile=newFileOutputStream(target);while(fromFile.read(cached)!=-1) {
toFile.write(cached);
}
toFile.flush();
toFile.close();
fromFile.close();newFile(source).deleteOnExit();
}/*** 把属性文件转换成Map
*
*@parampropertiesFile
*@return*@throwsException*/publicstaticfinalMapgetPropertiesMap(String propertiesFile)throwsException{
Properties properties=newProperties();
FileInputStream inputStream=newFileInputStream(propertiesFile);
properties.load(inputStream);
Mapmap=newHashMap();
SetkeySet=properties.keySet();for(Object key : keySet) {
map.put((String)key, properties.getProperty((String)key));
}returnmap;
}
@SuppressWarnings("unchecked")publicstaticfinalMapgetPropertiesMap(Class clazz,String fileName)throwsException{
Properties properties=newProperties();
InputStream inputStream=clazz.getResourceAsStream(fileName);if(inputStream==null)
inputStream=clazz.getClassLoader().getResourceAsStream(fileName);
properties.load(inputStream);
Mapmap=newHashMap();
SetkeySet=properties.keySet();for(Object key : keySet) {
map.put((String)key, properties.getProperty((String)key));
}returnmap;
}/*** 把属性文件转换成Map
*
*@paraminputStream
*@return*@throwsException*/publicstaticfinalMapgetPropertiesMap(InputStream inputStream)throwsException{
Properties properties=newProperties();
properties.load(inputStream);
Mapmap=newHashMap();
SetkeySet=properties.keySet();for(Object key : keySet) {
map.put((String)key, properties.getProperty((String)key));
}returnmap;
}/*** 把文本文件转换成String
*
*@paramfullPath
*@return*@throwsException*/publicstaticString readFile(String fullPath)throwsException{
BufferedReader reader=newBufferedReader(newFileReader(fullPath));if(reader==null)returnnull;
StringBuilder builder=newStringBuilder("");
String line=null;while((line=reader.readLine())!=null) {
builder.append(line+"\n");
}returnbuilder.toString();
}/*** 获取资源文件流
*
*@paramclazz
*@paramname
*@return*/@SuppressWarnings("unchecked")publicstaticInputStream getResourceAsStream(Class clazz,String name) {try{
InputStream inputStream=clazz.getResourceAsStream(name);if(inputStream==null)
inputStream=clazz.getClassLoader().getResourceAsStream(name);returninputStream;
}catch(Exception e) {if(log.isWarnEnabled())
log.warn("获取资源文件失败", e);returnnull;
}
}
}