工具类主要联合commons包对文件的递归筛选,文件复制等的操作,具体代码如下。
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CommonUtil
{
private static Logger LOG = LoggerFactory.getLogger(CommonUtil.class);
public static String getObjectToString(Object obj)
{
return getObjectToString(obj, "");
}
public static String getObjectToString(Object obj, String defaultStr)
{
if (obj == null) {
return defaultStr;
}
return obj.toString().trim();
}
public static <T> String getObjectToStringFromMap(Map<T, ?> map, T key)
{
if (map == null) {
return "";
}
return getObjectToStringFromMap(map, key, "");
}
public static <T> String getObjectToStringFromMap(Map<T, ?> map, T key, String defaultStr)
{
if (map == null) {
return "";
}
return getObjectToString(map.get(key), defaultStr);
}
public static void clearDirectoryPath(String pathStr)
{
LOG.warn("Begin to clear path : " + pathStr);
try
{
File path = new File(pathStr);
if (path.exists())
{
FileUtils.deleteDirectory(path);
if (path.exists()) {
throw new RuntimeException(String.format("clear directory failed, path is %s", new Object[] { pathStr }));
}
}
FileUtils.forceMkdir(path);
if (!path.exists()) {
throw new RuntimeException(String.format("create directory failed, path is %s", new Object[] { pathStr }));
}
}
catch (Throwable e)
{
throw new RuntimeException(e);
}
}
public static void forceDelete(String filePathStr)
{
try
{
File path = new File(filePathStr);
if (path.exists())
{
FileUtils.forceDelete(path);
if (path.exists()) {
throw new RuntimeException(String.format("delete file failed, path is %s", new Object[] { filePathStr }));
}
}
}
catch (Throwable e)
{
throw new RuntimeException(e);
}
}
public static void delAllFile(String path)
{
File file = FileUtils.getFile(new String[] { path });
File[] tempFileList = file.listFiles();
if (tempFileList == null)
{
file.delete();
file = null;
return;
}
File tempFile = null;
for (int i = 0; i < tempFileList.length; i++)
{
tempFile = tempFileList[i];
if (tempFile.isFile()) {
tempFile.delete();
}
if (tempFile.isDirectory())
{
delAllFile(tempFileList[i].getAbsolutePath());
tempFile.delete();
}
tempFile = null;
}
file.delete();
file = null;
}
public static boolean existsFileInDirectory(String path)
{
if (StringUtils.isEmpty(path)) {
return false;
}
File file = new File(path);
if (!file.exists()) {
return false;
}
if (file.isFile()) {
return true;
}
for (File f : file.listFiles())
{
if (f.isFile()) {
return true;
}
if ((f.isDirectory()) && (existsFileInDirectory(f.getAbsolutePath()))) {
return true;
}
}
return false;
}
public static List<String> findFilesInDirectory(String path)
{
List<String> files = new ArrayList();
if (StringUtils.isEmpty(path)) {
return files;
}
File file = new File(path);
if (!file.exists()) {
return files;
}
if (file.isFile())
{
files.add(file.getAbsolutePath());
return files;
}
for (File f : file.listFiles()) {
if (f.isFile()) {
files.add(f.getAbsolutePath());
} else if (f.isDirectory()) {
files.addAll(findFilesInDirectory(f.getAbsolutePath()));
}
}
return files;
}
public static List<String> findFilesinDirectoryBySuffix(String path, String[] suffixs)
{
List<String> fileList = new ArrayList();
if ((StringUtils.isEmpty(path)) || (suffixs == null) || (1 > suffixs.length)) {
return fileList;
}
List<String> findFileList = findFilesInDirectory(path);
for (String string : findFileList) {
if (findFileBySuffix(string, suffixs)) {
fileList.add(string);
}
}
return fileList;
}
private static boolean findFileBySuffix(String fileStr, String[] suffixs)
{
for (String suffix : suffixs) {
if (fileStr.toLowerCase().endsWith(suffix.toLowerCase())) {
return true;
}
}
return false;
}
public static List<String> findFilesinDirectoryByName(String path, String[] fileNames)
{
List<String> fileList = new ArrayList();
if ((StringUtils.isEmpty(path)) || (fileNames == null) || (1 > fileNames.length)) {
return fileList;
}
List<String> findFileList = findFilesInDirectory(path);
for (String string : findFileList) {
if (findFileByName(string, fileNames)) {
fileList.add(string);
}
}
return fileList;
}
private static boolean findFileByName(String fileStr, String[] fileNames)
{
for (String f : fileNames)
{
String fileName = new File(fileStr).getName();
if (fileName.equalsIgnoreCase(f)) {
return true;
}
}
return false;
}
public static String getLocalPath(String pathName)
{
File[] roots = File.listRoots();
if ((roots == null) || (1 > roots.length)) {
throw new RuntimeException("root file is not exists.");
}
String maxDevice = "";
long maxFreeSpace = 0L;
for (File rootFile : roots) {
if ((rootFile.canRead()) && (rootFile.canWrite())) {
if (rootFile.getFreeSpace() > maxFreeSpace)
{
maxFreeSpace = rootFile.getFreeSpace();
maxDevice = rootFile.getPath();
}
}
}
return String.format("%sLocal/%s/", new Object[] { maxDevice, pathName }).replace("\\", "/");
}
public static void movefile(String oriFile, String targetPath)
{
try
{
File target = new File(targetPath);
File srcFile = new File(oriFile);
if (target.isDirectory()) {
targetPath = targetPath + File.separator + srcFile.getName();
}
backupAndCopy(srcFile, targetPath);
}
catch (IOException e)
{
LOG.error(String.format("movefile %s to %s, Exception: %s", new Object[] { oriFile, targetPath, e.getLocalizedMessage() }));
}
}
public static void backupAndCopy(File srcFile, String targetName)
throws IOException
{
File destination = new File(targetName);
if (destination.exists())
{
destination.mkdirs();
FileUtils.moveFile(destination, new File(targetName + "." + System.currentTimeMillis()));
}
FileUtils.moveFile(srcFile, destination);
}
public static String subString(String strValue, String str)
{
if ((strValue != null) || ("".equals(strValue))) {
return strValue.substring(strValue.lastIndexOf(str) + 1);
}
return strValue;
}
public static String arrToString(List<Object> list)
{
StringBuilder bf = new StringBuilder("");
for (int i = 0; i < list.size(); i++) {
bf.append(list.get(i));
}
return bf.toString();
}
}