Java examples for File Path IO:Directory Search
Filter that accepts only files modified in the current day:
import java.io.IOException; import java.nio.file.DirectoryStream; 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);//from w ww. j a v a 2 s .c o m long modifiedTime = ((FileTime) Files.getAttribute(path, "basic:lastModifiedTime", LinkOption.NOFOLLOW_LINKS)) .to(TimeUnit.DAYS); if (currentTime == modifiedTime) { return true; } return false; } }; System.out.println("\nUser defined filter applied:"); try (DirectoryStream<Path> ds = Files.newDirectoryStream(path, time_filter)) { for (Path file : ds) { System.out.println(file.getFileName()); } } catch (IOException e) { System.err.println(e); } } }