How to get the list of only directories in a directory in java?

Here is an Example to explain to get the list of only directories in a directory in java.

package com.nourit.javaIO;

import java.io.File;

public class ListOnlyDirectoriesInDirectory {

  public static void main(String[] args) {
    File folder = new File("D:/tmp");
    File[] files = folder.listFiles();

    for (File file : files) {
      if (file.isDirectory()) {
        System.out.println(file.getName());
      }
    }
  }
}

Output:

nourit

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*