您的位置:首页 > 编程语言 > Java开发

Java获取目录下全部文件

2016-08-05 08:49 309 查看
今天用java统计一个目录下面有多少个txt文件,总是和windows下面的搜索结果对不上。后面把不是我的过滤条件的文件路径打印出来发现,
有几个是*.TXT的。经验不丰富呀。
下面附上自己写的类库。
package com.sunzhongxuan.main;

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

/**
* java中文件处理类,自己的一些积累
*
* @author zhongxuan_sun
*
*/
public class FileUtils {

/**
*
* @param pathName
*            文件夹路径
* @param maxDepth
*            需要遍历的文件夹深度
* @param suffix
*            文件后缀
* @param isNoCase
*            文件后缀是否区分大小写
* @return
*/
public static List<String> getAllAbsolutePath(String pathName, int maxDepth, String suffix, boolean isNoCase) {
if ( pathName == null || pathName.isEmpty() || maxDepth <= 0 || suffix == null || suffix.isEmpty())
return null;
int curDepth = 0;
return getAllAbsolutePath(pathName, maxDepth, curDepth, suffix, isNoCase, false);

}

/**
* 得到所有文件绝对路径,除了指定的后缀
*
* @param pathName
* @param maxDepth
* @param suffix
* @param isNoCase
* @return
*/
public static List<String> getAllAbsolutePathExcept(String pathName , int maxDepth , String suffix,
boolean isNoCase) {
if ( pathName == null || pathName.isEmpty() || maxDepth <= 0 || suffix == null || suffix.isEmpty())
return null;
int curDepth = 0;
return getAllAbsolutePath(pathName, maxDepth, curDepth, suffix, isNoCase, true);
}

private static List<String> getAllAbsolutePath(String pathName, int maxDepth, int curDepth, String suffix,
boolean isNoCase, boolean isExceptMode) {
List<String> result = new ArrayList<>();
if ( pathName == null || pathName.isEmpty() || maxDepth <= 0 || suffix == null || suffix.isEmpty())
return result;
// 达到最大文件夹深度直接返回
if ( maxDepth < curDepth)
return result;

File file = new File( pathName);
if ( file.isFile()) {
String fileAbsolutePath = file.getAbsolutePath();
boolean isNoCaseEndWithSuffix = isNoCase && endWithSuffixNoCase(suffix, pathName);
boolean isCaseEndWithSuffix = ! isNoCase && fileAbsolutePath.endsWith( suffix);
if (! isExceptMode) {
if ( isNoCaseEndWithSuffix) {
result.add( fileAbsolutePath);
}
if ( isCaseEndWithSuffix) {
result.add( fileAbsolutePath);
}
} else {
if (! isNoCaseEndWithSuffix && !isCaseEndWithSuffix) {
result.add( fileAbsolutePath);
}

}

return result;
} else if ( file.isDirectory()) {
++ curDepth;
File[] files = file.listFiles();
for ( int i = 0; i < files. length; ++ i) {
File curFile = files[ i];
result.addAll( getAllAbsolutePath(curFile.getAbsolutePath(), maxDepth, curDepth, suffix, isNoCase,
isExceptMode));
}
}

return result;
}

/**
* 判断绝对路径是否包含该后缀,不区分大小写
*
* @param suffix
* @param fileAbsolutePath
* @return
*/
private static boolean endWithS
4000
uffixNoCase(String suffix, String fileAbsolutePath) {
if ( suffix == null || suffix.isEmpty() || fileAbsolutePath == null || fileAbsolutePath.isEmpty())
return false;

if ( fileAbsolutePath.length() < suffix.length())
return false;

char[] suffixChars = suffix.toCharArray();
char[] fileAbsolutePathEndChars = new char[ suffixChars. length];
int srcBegin = fileAbsolutePath.length() - suffixChars. length;
int srcEnd = fileAbsolutePath.length();
fileAbsolutePath.getChars( srcBegin, srcEnd, fileAbsolutePathEndChars, 0);

for ( int i = 0; i < suffix.length(); ++ i) {
// 要么相等,要么相差32说明一个是大写一个是小写
if ( suffixChars[ i] == fileAbsolutePathEndChars[i]
|| (Math. abs(suffixChars [i ] - fileAbsolutePathEndChars[i]) == 32)) {
continue;
} else {
return false;
}
}

return true;

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: