2.3.1 在项目中查找文件

Demo:

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

/**在目录中查找文件:用模式匹配过滤dir项目中所有的.properties文件;
 * @author jinxing
 * @DirectoryStream DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.properties")
 * 过滤流中用到的模式匹配称为glob模式匹配——*.properties
 */
public class PathFindGlob {
    
    public static void main(String[] args) {
        
        // 提前准备好的目录及其中的三个文件:log4j.xml、shortUrl.properties、weixinjsapi.properties
        Path dir = Paths.get("/Users/jinxing/Documents/pathtest/path1");
        
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.properties")) {
            
            for (Path entry: stream) {
                System.out.println(entry.getFileName());
            }
            
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        
    }

}

Ran As Java Application:

shortUrl.properties
weixinjsapi.properties

 

posted @ 2016-01-06 15:29  springup  阅读(230)  评论(0)    收藏  举报