Java获取目录下全部文件

今天用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 endWithSuffixNoCase(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;

     }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值