集册 Java实例教程 只接受当天修改的文件的筛选器:

只接受当天修改的文件的筛选器:

欢马劈雪     最近更新时间:2020-01-02 10:19:05

547
仅接受当天修改的文件的过滤器:
import java.io.IOException;

import java.nio.file.DirectoryStream;//来自 时 代 J a v a - nowjava.com

import java.nio.file.Files;

import java.nio.file.LinkOption;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.attribute.FileTime;

import java.util.concurrent.TimeUnit;


public class Main {

  public static void main(String[] args) {

    Path path = Paths.get("C:/folder1/folder2/folder4");

    DirectoryStream.Filter<Path> time_filter = new DirectoryStream.Filter<Path>() {


      public boolean accept(Path path) throws IOException {

        long currentTime = FileTime.fromMillis(System.currentTimeMillis()).to(

            TimeUnit.DAYS);

        long modifiedTime = ((FileTime) Files.getAttribute(path,

            "basic:lastModifiedTime", LinkOption.NOFOLLOW_LINKS))/*from 时代Java - N o w  J a v a . c o m*/

            .to(TimeUnit.DAYS);

        if (currentTime == modifiedTime) {

          return true;

        }


        return false;

      }

    };


    System.out.println("\nUser defined filter applied:");

    
展开阅读全文