Skip to content

Commit

Permalink
fix fileFinder warin
Browse files Browse the repository at this point in the history
  • Loading branch information
孙健 committed Dec 15, 2016
1 parent 964c19d commit a2d7ed7
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions src/main/java/org/nlpcn/commons/lang/util/FileFinder.java
@@ -1,36 +1,42 @@
package org.nlpcn.commons.lang.util;

import java.io.File;
import java.security.AccessControlException;
import java.util.ArrayList;
import java.util.List;

import org.nlpcn.commons.lang.util.logging.Log;
import org.nlpcn.commons.lang.util.logging.LogFactory;

/**
* 从系统各个环境中找文件.或者文件夹
*
* @author ansj
*/
public class FileFinder {

private static final Log LOG = LogFactory.getLog();

/**
* 系统路径分隔符
*/
private static final String SEPARATOR = System.getProperty("path.separator");
private static final String[] PATHS_PROPERTIES = new String[]{ "java.class.path", "java.library.path"};
private static final String[] PATHS_PROPERTIES = new String[] { "java.class.path", "java.library.path" };

public static List<File> fileDir = new ArrayList<File>();

static{
fileDir.add(new File("").getAbsoluteFile()) ;
static {
fileDir.add(new File("").getAbsoluteFile());
}


/**
* 输入一个文件名或者文件的最后路径寻找文件
* default deep Integer.max
* 输入一个文件名或者文件的最后路径寻找文件 default deep Integer.max
*
* @param
* @return
*/
public static File find(String lastPath) {
return find(lastPath,Integer.MAX_VALUE) ;
return find(lastPath, Integer.MAX_VALUE);
}

/**
Expand All @@ -39,12 +45,12 @@ public static File find(String lastPath) {
* @param
* @return
*/
public static File find(String lastPath , int deep) {
public static File find(String lastPath, int deep) {

// 先深度查找
for (File file : fileDir) {
if (file.exists() && file.canRead()) {
file = findByFile(file, lastPath,deep);
file = findByFile(file, lastPath, deep);
if (file != null) {
return file;
}
Expand All @@ -55,17 +61,21 @@ public static File find(String lastPath , int deep) {
String[] propertyPath = System.getProperty(pathProperties).split(SEPARATOR);
for (String path : propertyPath) {
File file = new File(path);
if (file.exists() && file.canRead()) {
file = findByFile(file, lastPath,deep);
if (file != null) {
return file;
try {
if (file.canRead() && file.exists()) {
file = findByFile(file, lastPath, deep);
if (file != null) {
return file;
}
}
} catch (AccessControlException e) {
LOG.info(path + " not access to visit");
}
}
}
return null;
}

/**
* 根据一个文件深度查找
*
Expand All @@ -74,8 +84,8 @@ public static File find(String lastPath , int deep) {
* @param deep integer.max
* @return
*/
public static File findByFile(File file, String lastPath){
return findByFile(file, lastPath, Integer.MAX_VALUE) ;
public static File findByFile(File file, String lastPath) {
return findByFile(file, lastPath, Integer.MAX_VALUE);
}

/**
Expand All @@ -86,8 +96,8 @@ public static File findByFile(File file, String lastPath){
* @param deep
* @return
*/
public static File findByFile(File file, String lastPath,int deep) {
if (deep==0||!file.exists() || !file.canRead()) {
public static File findByFile(File file, String lastPath, int deep) {
if (deep == 0 || !file.exists() || !file.canRead()) {
return null;
}
if (file.getAbsolutePath().endsWith(lastPath)) {
Expand All @@ -98,7 +108,7 @@ public static File findByFile(File file, String lastPath,int deep) {
File[] listFiles = file.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file2 : listFiles) {
File temp = findByFile(file2, lastPath, deep-1);
File temp = findByFile(file2, lastPath, deep - 1);
if (temp != null) {
return temp;
}
Expand All @@ -108,5 +118,4 @@ public static File findByFile(File file, String lastPath,int deep) {
return null;
}


}

0 comments on commit a2d7ed7

Please sign in to comment.