package util;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* FileUtils.java
* description:文件处理工具类
* @author 魏霖涛
* @version $Revision: 1.3 $ $Date: 2017/02/06 07:02:48 $ $Author: weilintao $
* history 1.0.0 2016-1-6 created by weilintao
*/
public class FileUtils {
/**
* 删除文件
* @param filepath
*/
public static void deleteFile(String filepath) {
File file = new File(filepath);
if (file.exists()) {
file.delete();
}
}
/**
* 删除文件夹
*
* @param folderPath 文件夹完整绝对路径
*
*/
public static void delFolder(String folderPath) {
try {
// 删除完里面所有内容
delAllFile(folderPath);
String filePath = folderPath;
filePath = filePath.toString();
File myFilePath = new File(filePath);
// 删除空文件夹
myFilePath.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除指定文件夹下所有文件
*
* @param path 文件夹完整绝对路径
*
*
*/
public static boolean delAllFile(String path) {
boolean bea = false;
File file = new File(path);
if (!file.exists()) {
return bea;
}
if (!file.isDirectory()) {
return bea;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
// 先删除文件夹里面的文件
delAllFile(path + "/" + tempList[i]);
// 再删除空文件夹
delFolder(path + "/" + tempList[i]);
bea = true;
}
}
return bea;
}
public static void deleteFile(File file) {
if (file.exists()) {
file.delete();
}
}
/**
* 新建目录
* @param directory 目录路径 如: e:/
*/
public static void createFileDirectory(String directory) {
File file = new File(directory);
if (!file.exists()) {
//几级目录没有就建立几级 mkdir:只能建立第一级
file.mkdirs();
}
}
/**
* 复制单个文件
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/
* @param filename String 目标文件名 如:fqf.txt
* boolean
*/
public static void copyFile(String oldPath, String newPath, String filename) {
InputStream inStream = null;
FileOutputStream fs = null;
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
//文件存在时
if (oldfile.exists()) {
//读入原文件
inStream = new FileInputStream(oldPath);
//createFileDirectory(newPath);
fs = new FileOutputStream(newPath + filename);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
//字节数 文件大小
bytesum += byteread;
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fs != null) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static boolean fileExist(String filepath) {
File file = new File(filepath);
if (file.exists()) {
return true;
}
return false;
}
public static boolean directoryExist(String filepath) {
File file = new File(filepath);
if (file.exists()) {
return true;
}
return false;
}
/**
* 判断是否有常见的特殊符号
* @param str
* @return
*/
public static boolean containSpecialMark(String str) {
String regEx = "[\\\\/:\"*?<>|]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.find();
}
/**
* 复制文件 复制文件夹时候用
* @param sourcefile
* @param targetFile
* @throws IOException
*/
public static void copyFile(File sourcefile, File targetFile) {
try {
//新建文件输入流并对它进行缓冲
FileInputStream input = new FileInputStream(sourcefile);
BufferedInputStream inbuff = new BufferedInputStream(input);
//新建文件输出流并对它进行缓冲
FileOutputStream out = new FileOutputStream(targetFile);
BufferedOutputStream outbuff = new BufferedOutputStream(out);
//缓冲数组
byte[] b = new byte[1024 * 5];
int len = 0;
while ((len = inbuff.read(b)) != -1) {
outbuff.write(b, 0, len);
}
//刷新此缓冲的输出流
outbuff.flush();
//关闭流
inbuff.close();
outbuff.close();
out.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 复制文件夹里面的内容到另外一个文件夹
* @param sourceDir 源文件夹 D:/min_res/video/1565"
* @param targetDir 目标文件夹 D:/min_res/video/15651" 这个文件夹允许不存在
* @throws IOException
*/
public static void copyDirectiory(String sourceDir, String targetDir) {
try {
//新建目标目录
(new File(targetDir)).mkdirs();
//获取源文件夹当下的文件或目录
File[] file = (new File(sourceDir)).listFiles();
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
//源文件
File sourceFile = file[i];
//目标文件
File targetFile = new File(new File(targetDir).getAbsolutePath() + File.separator + file[i].getName());
copyFile(sourceFile, targetFile);
}
if (file[i].isDirectory()) {
//准备复制的源文件夹
String dir1 = sourceDir + "/" + file[i].getName();
//准备复制的目标文件夹
String dir2 = targetDir + "/" + file[i].getName();
copyDirectiory(dir1, dir2);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 1
- 2
- 3
前往页