按文件名、更新时间等查找本地文件夹中文件

文件过滤器,按照文件名称与更新时间过滤

/**
     * 文件过滤器
     * @Description
     * 类描述:
     * @author gao
     * @Date 2017年7月19日
     * @modify
     * 修改记录:
     *
     */
    static class FileByCondition implements FileFilter {
        private String fileName;
        private String endTime;
        private String startTime;

        @Override
        public boolean accept(File fileDir) {
            String fileNameDir = fileDir.getName();
            Date modifiedTime = new Date(fileDir.lastModified());
            String modifiedTimeStr = DateUtils.getDateTime(modifiedTime);
            int startFlag = 1;
            if (StringUtils.isNotBlank(startTime)) {
                startFlag = DateUtils.compareStrDateTime(modifiedTimeStr, startTime);
            }
            int endFlag = 1;
            if (StringUtils.isNotBlank(endTime)) {
                endFlag = DateUtils.compareStrDateTime(endTime, modifiedTimeStr);
            }
            boolean fileNameFlag = true;
            if (StringUtils.isNotBlank(fileName)) {
                fileNameFlag = fileNameDir.contains(fileName);
            }
            if (fileNameFlag && startFlag > 0 && endFlag > 0)
                return true;
            else
                return false;
        }

        public String getFileName() {
            return fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }

        public String getStartTime() {
            return startTime;
        }

        public void setStartTime(String startTime) {
            this.startTime = startTime;
        }

        public String getEndTime() {
            return endTime;
        }

        public void setEndTime(String endTime) {
            this.endTime = endTime;
        }

        public FileByCondition(String fileName, String startTime, String endTime) {
            this.fileName = fileName;
            this.startTime = startTime;
            this.endTime = endTime;
        }
    }

将过滤后的文件按照更新时间排序

    /**
     * 根据更新时间比较文件
     * @Description
     * 类描述:
     * @author gao
     * @Date 2017年7月18日
     * @modify
     * 修改记录:
     *
     */
    static class CompratorByModifiedTime implements Comparator<File> {
        public int compare(File f1, File f2) {
            long diff = f1.lastModified() - f2.lastModified();
            if (diff < 0)
                return 1;
            else if (diff == 0)
                return 0;
            else
                return -1;
        }

        public boolean equals(Object obj) {
            return true;
        }
    }

取得文件需要的属性信息

public static List<Map<String, String>> getFileByFilter(FileByCondition fileCondition) {
        String fileDirPath = "D:\\tools\\Tomcat\\Tomcat7\\apache-tomcat-7.0.57\\logs\\urs";
        //获取window或linux下的日志文件目录
        /*String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {
            fileDirPath = System.getProperty("catalina.home") + "\\logs\\urs\\";
        } else {
            fileDirPath = System.getProperty("catalina.home") + "/logs/urs/";
        }*/
        File fileDir = new File(fileDirPath);
        //日志目录下的所有日志文件
        File[] files = fileDir.listFiles(fileCondition);
        //按照修改时间排序   
        Arrays.sort(files, new CompratorByModifiedTime());
        List<Map<String, String>> resultList = new ArrayList<Map<String, String>>();
        for (int i = 0; i < files.length; i++) {
            Map<String, String> fileInfo = new HashMap<String, String>();
            File file = files[i];
            //文件名
            String fileName = file.getName();
            //修改时间
            String modifiedTime = DateUtils.getDateTime(new Date(file.lastModified()));
            //文件大小
            long fileLength = (long) Math.ceil((file.length() / 1024f));
            String fileSize = fileLength + "KB";
            fileInfo.put("fileName", fileName);
            fileInfo.put("fileTime", modifiedTime);
            fileInfo.put("fileSize", fileSize);
            resultList.add(fileInfo);
        }
        return resultList;
    }

main方法测试

public static void main(String[] args) {
        String fileNameCondition = "urs";
        String startTime = "2017-07-11 18:00:58";
        String endTime = "2017-07-29 18:01:00";
        FileByCondition fileCondition = new FileByCondition(fileNameCondition, startTime, endTime);
        List<Map<String, String>> resultList = getFileByFilter(fileCondition);
        System.out.println(resultList.size());
    }

 

posted on 2017-07-20 10:08  JoUU  阅读(1478)  评论(0编辑  收藏  举报

导航