0

i'm trying to use Files.list() api to list only folders, what are the missing lines that i need to add to my code in order to do that?

I have tried the files.walk method, but it didn't work. I would rather to stick to Files.list()

String dirName = ".";
try {
    Files.list(Paths.get(dirName)).forEach(System.out::  println);
} catch (IOException ex) {
    Logger.getLogger(ActiveDirectory.class.getName()).log(Level.SEVERE, null, ex);
}

I wanted the output to be only folders, but the output is everything including folders 

.\build
.\build.xml
.\manifest.mf
.\nbproject
.\src
.\test
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • [Here](https://stackoverflow.com/questions/5125242/java-list-only-subdirectories-from-a-directory-not-files) are some good hints how to do that! – csabinho Jul 31 '19 at 06:16
  • 1
    Provide a check with `Files.isDirectory(Path path);` – deHaar Jul 31 '19 at 06:16
  • Well, a quick google found [this](http://zetcode.com/articles/javalistdirectory/) and [this](https://www.mkyong.com/java/java-how-to-list-all-files-in-a-directory/) – MadProgrammer Jul 31 '19 at 06:19
  • 1
    @IbrahimAltaha First, backup. Take a long hard look at the examples - yes, they use Files.walk, but the underlying principle of filtering the results is the same, I’m sorry if it’s “too hard” and all you want is a “copy and paste” answer, because sometimes you won’t get exactly what you need, but you’ll get enough ideas to make a solution yourself and learn something in the process – MadProgrammer Jul 31 '19 at 06:37
  • @deHaar , do you mean i create a list , then populate this list, then run a counter loop for every line in that list whether its a directory or not? – Ibrahim Altaha Jul 31 '19 at 06:39
  • You don't really need a counter loop, you can create a list and then remove everything that is not a directory by something like `list.removeIf(path -> !Files.isDirectory(path));` Just check the answer given by @STaefi for a decent solution. – deHaar Jul 31 '19 at 06:42
  • @MadProgrammer, thanks again, i just thought there must be a single small tweak to the code. like adding one line or two. – Ibrahim Altaha Jul 31 '19 at 06:48

2 Answers2

1

I just post this since I saw a long trail of comments are going to be made under the question.

You can filter the directories using Files::isDirectory:

Files.list(Paths.get(dirName))
      .filter( Files::isDirectory )
      .forEach(System.out::println);

Or using File#listFiles which accepts a boolean filter:

File[] directories = new File(dirName).listFiles( f -> f.isDirectory() );
Arrays.stream( directories ).forEach( System.out::println );

But you should take the MadProgrammer's advice to make your solution out of the other solutions. It is not always this easy for someone to post your desired solution which you can use out of the box.

STaefi
  • 4,297
  • 1
  • 25
  • 43
0

You have to filter out the files by using the Files.isDirectory :

Files.list(Paths.get(dirName))
    .filter(path -> Files.isDirectory(path))
    .forEach(System.out::  println);
Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63
Dinesh
  • 1,046
  • 1
  • 8
  • 17