java-如何从具有多个不同日期的文件的文件夹中获取特定日期的所有文件? [英] How to get all files of particular date modified from a folder with numerous files of different date ?using java code

查看:329
本文介绍了java-如何从具有多个不同日期的文件的文件夹中获取特定日期的所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Blockquote

Blockquote

我想根据文件的日期和时间过滤文件,但是我在java中找不到任何方法. 如果有任何想法,请帮忙.File fil = new File("C:\ sujeet \ efsfiles \ iems \ input"); FilenameFilter filter = new FilenameFilter(){ 公共布尔值接受(文件fil,字符串名称){

I want to filter the files according to its date and time ,but i couldn't find any method in java . please help if any one has any idea about it.`File fil =new File("C:\sujeet\efsfiles\iems\input"); FilenameFilter filter =new FilenameFilter(){ public boolean accept(File fil, String name){

 return name.
}};
File[] temp= fil.listFiles(filter);
public void SeeFiles(){
for(File file : temp){

 if(file.isFile())  {
    count++;
     System.out.println(file.getName());


 }`

推荐答案

此处FileNameFilter在您要查找文件属性级别时不起作用

The FileNameFilter here won't work as you are looking for the file attribute level

检查是否有帮助

package com.tmp;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Tmp implements DirectoryStream.Filter<Path> {

    public static void main( String[] args ) {

        Path dir = Paths.get( "d:\\tmp" ); // Folder to search for files

        try (DirectoryStream<Path> stream = Files.newDirectoryStream( dir, new Tmp() )) {

            for ( Path entry : stream ) {

                System.out.println( entry.getFileName() ); // file name which matched the accessed date
            }

        } catch ( IOException x ) {

            System.err.println( x );
        }
    }

    @Override
    public boolean accept( Path file ) throws IOException {

        try {

            BasicFileAttributes attr = Files.readAttributes( file, BasicFileAttributes.class );

            Date fileLastAccessedDate = new Date( attr.lastModifiedTime().toMillis() );

            Calendar cal = Calendar.getInstance();

            cal.setTime( fileLastAccessedDate );

            cal.set( Calendar.HOUR_OF_DAY, 0 );
            cal.set( Calendar.SECOND, 0 );
            cal.set( Calendar.MINUTE, 0 );
            cal.set( Calendar.MILLISECOND, 0 );

            SimpleDateFormat sdf = new SimpleDateFormat( "MM-dd-yyyy" );

            Date targetDate = sdf.parse( "2-20-2017" ); // date looking for the files

            return ( cal.getTime().equals( targetDate ) );

        } catch ( Exception x ) {

            System.err.println( x );

            return false;
        }
    }
}

这篇关于java-如何从具有多个不同日期的文件的文件夹中获取特定日期的所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆