Java - Read all .txt files in folder

后端 未结 7 1849
醉话见心
醉话见心 2020-12-08 15:49

Let\'s say, I have a folder called maps and inside maps I have map1.txt, map2.txt, and map3.txt. How can I u

相关标签:
7条回答
  • 2020-12-08 16:27

    If you want a better way of doing this using the new java.nio api, then this is the way, taken from the java docs

    Path dir = ...;
    try (DirectoryStream<Path> stream =
         Files.newDirectoryStream(dir, "*.txt")) {
        for (Path entry: stream) {
            System.out.println(entry.getFileName());
        }
    } catch (IOException x) {
        // IOException can never be thrown by the iteration.
        // In this snippet, it can // only be thrown by newDirectoryStream.
        System.err.println(x);
    }
    
    0 讨论(0)
提交回复
热议问题