import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FileUtil {
private static Logger logger = LoggerFactory.getLogger(FileUtil.class);
/**
* 根据路径获取文件名
*
* @param Path
* @return
* @author liuying
* @date 2015年1月23日上午10:31:17
*/
public static String getFileName(String path) {
if (path.equals(""))
return "";
int index = path.lastIndexOf(File.separator);
if (index < 0)
return path;
return path.substring(index + 1);
}
/**
* 根据路径
*
* @param Path
* @return
* @author liuying
* @date 2015年1月23日上午10:31:17
*/
public static String getFilePathWithoutFileName(String path) {
if (path.equals(""))
return "";
int index = path.lastIndexOf(File.separator);
if (index < 0)
return path;
return path.substring(0,index+1);
}
/**
* 删除单个文件
*
* @param Path
* @author liuying
* @date 2015年1月23日上午10:31:53
*/
public static void deleteFile(String path) {
File file = new File(path);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
}
}
/**
* 根据文件路径获取后缀名
*
* @param filePath
* @param suffix
* @return
* @throws IOException
* @author liuying
* @date 2015年1月23日上午10:51:12
*/
public static String getFileSuffixName(String filePath) throws IOException {
File file = new File(filePath);
String filename = file.getName();
String extName = filename.substring(filename.lastIndexOf(".") + 1);
if (extName.indexOf("?") > 0) {
extName = extName.substring(0, extName.indexOf("?")).toLowerCase();
}
return extName;
}
/**
* 根据文件路径获取后缀名
*
* @param filePath
* @param suffix
* @return
* @throws IOException
* @author liuying
* @date 2015年1月23日上午10:51:12
*/
public static String getFileSuffixName(String filePath, String suffix) throws IOException {
String extName = getFileSuffixName(filePath);
if (StringUtils.isBlank(suffix)) {
return extName;
} else {
String extNames = PropertiesUtil.getProperty(suffix);
if (extNames.indexOf(extName) > 0) {
return extName;
} else {
logger.error("不在允许范围内的文件后缀名,请检查!filePath:" + filePath);
return "";
}
}
}
/**
* 检查绝对路径的文件是否存在
*
* @param absolutePath
* @return
* @author liuying
* @date 2015年1月23日上午10:54:55
*/
public static boolean isFileExists(String absolutePath) {
boolean isExists = false;
try {
File file = new File(absolutePath);
isExists = file.exists();
} catch (Exception e) {
e.printStackTrace();
logger.error("判断指定文件是否存在的操作出现错误!filePhysicalPath=" + absolutePath);
}
return isExists;
}
/**
* 将源文件的内容拷贝到目标文件中
*
* @param srcFile
* @param targetFile
* @author liuying
* @date 2015年1月23日上午11:02:07
*/
public static void copyFile(String srcFile, String targetFile) {
if (!(isFileExists(srcFile))) {
logger.error("用于将源文件的内容拷贝到目标文件中的操作失败!失败原因:源文件不存在!srcFile:" + srcFile);
return;
}
FolderUtil.createFolder(FolderUtil.getFolderPhysicalPath(targetFile));
try {
FileInputStream input = new FileInputStream(srcFile);
FileOutputStream output = new FileOutputStream(targetFile);
byte[] b = new byte[1024 * 5];
int len;
try {
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
} catch (IOException e) {
e.printStackTrace();
logger.error("将源文件的内容拷贝到目标文件中的操作失败,失败发生在拷贝的过程中!源文件:" + srcFile + ",目标文件:" + targetFile);
} finally {
output.close();
input.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
logger.error("将源文件的内容拷贝到目标文件中的操作失败,原因是找不到指定的文件!源文件:" + srcFile + ",目标文件:" + targetFile);
} catch (Exception e) {
e.printStackTrace();
logger.error("将源文件的内容拷贝到目标文件中的操作失败!源文件:" + srcFile + ",目标文件:" + targetFile);
}
logger.debug("文件拷贝成功!源文件:" + srcFile + ",目标文件:" + targetFile);
}
/**
* 遍历文件夹下的文件,并根据规则修改文件名称
*
* @param directoryPath
* 文件夹绝对路径
* @author liuying
* @date 2015年1月23日上午11:03:09
*/
public static void reFileName(String directoryPath) {
File dir = new File(directoryPath);
File[] files = dir.listFiles();
if (files == null) {
logger.error("遍历文件夹下的文件,并根据规则修改文件名称的操作失败!原因是提供的文件夹不存在!directoryPath:" + directoryPath);
return;
} else {
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
continue;
} else {
String fileNum;
// if (files[i].getName().indexOf("_") > 0)
// fileNum = files[i].getName().substring(0,
// files[i].getName().indexOf("_"));
// else
// fileNum = files[i].getName().substring(0,
// files[i].getName().indexOf("."));
fileNum = (i + 1) + "";
StringBuffer newName = new StringBuffer();
newName.append(directoryPath);
if (fileNum.length() == 2)
newName.append("0");
if (fileNum.length() == 1)
newName.append("00");
newName.append(fileNum).append(".XML");
File dest = new File(newName.toString());
files[i].renameTo(dest);
newName = null;
fileNum = null;
}
}
}
}
/**
* 读取文件,将内容以字符串返回
*
* @param filePhysicalPath
* @param charset
* @return
* @author liuying
* @date 2015年1月23日上午11:05:09
*/
public static String readFileContent(String absolutePath, String charset) {
String content = "";
StringBuffer sbf = new StringBuffer();
File file = new File(absolutePath);
if (file.exists()) {
if (file.isFile()) {
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
fis = new FileInputStream(absolutePath);
isr = new InputStreamReader(fis, charset);
br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
sbf.append(line);
sbf.append("\r\n");
}
} catch (Exception e) {
e.printStackTrace();
logger.error("读取文件中的内容,将内容赋予一个字符串中的操作出错!filePhysicalPath:" + absolutePath);
} finally {
try {
br.close();
isr.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
logger.error("读取文件中的内容,将内容赋予一个字符串中的操作在关闭时文件读取类时出错!");
}
}
content = sbf.toString();
// 去掉最后一行多余的"\r\n"
if (content.length() > 2)
content = content.substring(0, content.length() - 2);
} else {
logger.error("读取文件中的内容,将内容赋予一个字符串中的操作出错!出错原因:提供的文件不是文件类型:filePhysicalPath:" + absolutePath);
}
} else {
logger.error("读取文件中的内容,将内容赋予一个字符串中的操作出错!出错原因:不存在提供的文件:filePhysicalPath:" + absolutePath);
}
return content;
}
}