集册 Java实例教程 通过应用用户列出内容

通过应用用户列出内容

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

510
要编写自己的筛选器,请实现DirectoryStream.filter<T>接口。
/*
n  o  w  j  a  v  a . c o m
*/

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;


public class Main {

  public static void main(String[] args) {


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


    // user-defined filter - only directories are accepted

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


      public boolean accept(Path path) throws IOException {

        return (Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS));

      }

    };


    System.out.println("\nUser defined filter applied:");
    /**
    n o w    j a v a  . c o m
    **/


    try (DirectoryStream<Path> ds = Files.newDir
展开阅读全文