Path、Paths和DirectoryStream<T>

Windows的D:luceneexample下有a.txt、b.txt、c.txt和d.docx四个文件。

遍历出txt文件:

package com.ant.jdk7;

import java.io.File;
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 PathDemo {
    public static void main(String[] args) throws IOException {
        PathDemo pathDemo = new PathDemo();
        pathDemo.ergodicDir("D:"+ File.separator+"lucene"+File.separator+"example");
    }

    public void ergodicDir(String dir) throws IOException {
        Path path = Paths.get(dir);
        DirectoryStream<Path> paths = Files.newDirectoryStream(path,"*.txt");
        for(Path p : paths){
            System.out.println(p.getFileName());
        }
    }
}

原文地址:https://www.cnblogs.com/i-hard-working/p/9567987.html