FileTool (文件工具类)

public class FileTool {


private static final Log log = LogFactory.getLog(FileTool.class);
private static final int BUFFER_SIZE = 16 * 1024 ;


/**
* 删除目录和文件
*
* @param fi
* @throws IOException
*/
public static boolean deleteDirectory(File fi) throws IOException {
if ((null != fi) && (fi.isDirectory() || fi.isFile())) {
// 如果fi是文件直接删除
if (fi.isFile()) {
fi.delete();
return true;
} else { // 如果是目录,获取该目录下的所有文件和文件夹
File[] files = fi.listFiles();
int sz = files.length;
for (int i = 0; i < sz; i++) {
if (files[i].isDirectory()) {
deleteDirectory(files[i]);
} else {
files[i].delete();
}
}
}
return true;
} else {
log.error("文件或目录删除失败", new IllegalArgumentException("异常信息: " + fi
+ " 不是一个目录或文件 "));
throw new IllegalArgumentException("异常信息: " + fi + " 不是一个目录或文件 ");
}
}


public static boolean deleteDirectoryAndParent(File fi) throws IOException {
if ((null != fi) && (fi.isDirectory() || fi.isFile())) {
// 如果fi是文件直接删除
if (fi.isFile()) {
fi.delete();
return true;
} else { // 如果是目录,获取该目录下的所有文件和文件夹
File[] files = fi.listFiles();
int sz = files.length;
for (int i = 0; i < sz; i++) {
if (files[i].isDirectory()) {
deleteDirectoryAndParent(files[i]);
} else {
files[i].delete();
}
}
}
fi.delete();
return true;
} else {
log.error("delete failed");
throw new IllegalArgumentException("异常信息: " + fi + " 不是一个目录或文件 ");
}
}
/**
* 根据路径删除文件
* @param filename
* @return
*/
public static boolean deleteFile(String filename){
File file = new File(filename);
if(file.exists()){
return file.delete();
}
return false;
}

/**
* 创建目录
* @param directoryName
* @return boolean
* @since Jan 6, 2010
*/
public static boolean createDirectory(String directoryName){
File file=new File(directoryName);
try {
return file.mkdirs();
} catch (Exception e) {
log.error("create directory failed",e);
return false;
}
}

/**
* 文件复制
* @param src 源文件
* @param dst 目标文件
* @since Jan 21, 2010
*/
public static void copy(File src, File dst) {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 根据多媒体文件格式,获取图片文件类型
* @param mimeType 多媒体文件类型
* @return String
* @since Jan 21, 2010
*/
public static String getPicFileType(String mimeType){
try {
String typename = "";
if (mimeType.equals("image/pjpeg")) {
typename = ".jpg";
} else if (mimeType.equals("image/bmp")) {
typename = ".bmp";
} else if (mimeType.equals("image/png")) {
typename = ".png";
} else if (mimeType.equals("image/gif")) {
typename = ".gif";
} else if (mimeType.equals("image/jpeg")) {
typename = ".jpg";
} else if (mimeType.equals("image/jpg")) {
typename = ".jpg";
} else if (mimeType.equals("image/x-png")) {
typename = ".png";
}
return typename;
} catch (Exception e) {
log.error("get pic file type failed",e);
}
return null;
}

/**
* 修改当前系统毫秒时间
* @param fileType
* @return String
* @since Jan 22, 2010
*/
public static String getMTimeFileName(String fileType){
try {
Calendar cal=Calendar.getInstance();
long mTime=cal.getTimeInMillis();
return String.valueOf(mTime)+fileType;
} catch (Exception e) {
log.info("get Time In Millis to change the file name failed",e);
}
return null;
}

/**
* 获取以长日期命名的文件名称
* @param fileName
* @return String
* @since Jan 22, 2010
*/
@SuppressWarnings("static-access")
public static String getLongDataFileName(String fileType){
try {
Calendar calendar=Calendar.getInstance();
StringBuffer fileName=new StringBuffer();
fileName.append(calendar.get(calendar.YEAR));
fileName.append(calendar.get(calendar.MONTH));
fileName.append(calendar.get(calendar.DATE));
fileName.append(calendar.get(calendar.HOUR_OF_DAY));
fileName.append(calendar.get(calendar.MINUTE));
fileName.append(calendar.get(calendar.SECOND));
fileName.append(calendar.get(calendar.MILLISECOND));
fileName.append(fileType);
return fileName.toString();
} catch (Exception e) {
log.info("get long data to change file name failed",e);
}
return null;
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值