How to get contents of a folder and put into an ArrayList

后端 未结 5 1045
滥情空心
滥情空心 2020-12-14 06:10

I want to use

File f = new File(\"C:\\\\\");

to make an ArrayList with the contents of the folder.

I am not very good with buffered

5条回答
  •  暖寄归人
    2020-12-14 06:36

    Streams are fast and very easy to read:

    final Path rootPath = Paths.get("/tmp");
    
    // All entries Path objects
    ArrayList all = Files
            .list(rootPath)
            .collect(Collectors.toCollection(ArrayList::new));
    
    // Only regular files at Path objects
    ArrayList regularFilePaths = Files
            .list(rootPath)
            .filter(Files::isRegularFile)
            .collect(Collectors.toCollection(ArrayList::new));
    
    // Only regular files as String paths
    ArrayList regularPathsAsString = Files
            .list(rootPath)
            .filter(Files::isRegularFile)
            .map(Path::toString)
            .collect(Collectors.toCollection(ArrayList::new));
    

提交回复
热议问题