从类路径中获取文件列表

时间:2019-05-11 01:12:19

标签: java file

我正在尝试测试实用程序方法。

我需要从项目的ressource目录中创建文件列表。

我可以一次创建一个文件,但是我需要遍历目录中的所有文件。

我的测试方法如下:

 @Test
    public void testFileRessourceLoader(){
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        File file = new File(loader.getResource("exacts/graph_968_26_8.lst").getFile());
        System.out.println(file.getName());
    }

我要读取的文件的结构。我需要一种不键入程序中每个文件名称的方法。该解决方案应该很简单,并且不依赖于某些库。

enter image description here

1 个答案:

答案 0 :(得分:1)

Path exacts = Paths.get(getClass().getResource("/exacts").toURI());
Stream<Path> files = Files.list(exacts);
files.forEach(p -> System.out.println(p.getName()));