Filter that accepts only files/directories larger than 200KB: - Java File Path IO

Java examples for File Path IO:Directory Search

Description

Filter that accepts only files/directories larger than 200KB:

Demo Code

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) {
    Path path = Paths.get("C:/folder1/folder2/folder4");
    DirectoryStream.Filter<Path> size_filter = new DirectoryStream.Filter<Path>() {

      public boolean accept(Path path) throws IOException {
        return (Files.size(path) > 204800L);
      }//from ww  w .ja  v a2 s.  c  om
    };
    System.out.println("\nUser defined filter applied:");
    try (DirectoryStream<Path> ds = Files.newDirectoryStream(path, size_filter)) {
      for (Path file : ds) {
        System.out.println(file.getFileName());
      }
    } catch (IOException e) {
      System.err.println(e);
    }
  }
}

Result


Related Tutorials