Thursday 10, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Monday, 22 May 2017

How to retrieve all the directory names from Directory using Java

File[] java.io.File.listFiles(FileFilter filter) 
Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter. The behavior of this method is the same as that of the listFiles() method, except that the pathnames in the returned array must satisfy the filter. If the given filter is null then all pathnames are accepted. Otherwise, a pathname satisfies the filter if and only if the value true results when the FileFilter.accept(File) method of the filter is invoked on the pathname.

Parameters:
filter A file filter

Returns:
An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs. Throws: SecurityException - If a security manager exists and its SecurityManager.checkRead(String) method denies read access to the directory

Since:
1.2

See Also:
java.nio.file.Files.newDirectoryStream(Path, java.nio.file.DirectoryStream.Filter)

DirectoryNames.java
package com.javatutorialcorner.directory;

import java.io.File;
import java.io.FileFilter;

public class DirectoryNames {
 private static final String DIRECOTORY_PATH = "C:\\JTC\\Java";

 public static void main(String args[]) {
  searchDirectory();
 }

 private static void searchDirectory() {
  File directory = new File(DIRECOTORY_PATH);

  FileFilter fileFilter = new FileFilter() {
   public boolean accept(File dir) {
    return dir.isDirectory();
   }
  };

  File[] files = directory.listFiles(fileFilter);
  if (files.length == 0) {
   System.out.println("Directory does not exist");
  } else {
   for (int i = 0; i < files.length; i++) {
    File filename = files[i];
    System.out.println(filename.toString());
   }
  }
 }
}
Output

C:\JTC\Java\Example 
C:\JTC\Java\Examples

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to add value to all types of Collections Following example shows how to add values in all types collections in Java. Sample Program AddValueToCollection.java package com.javatutorialcorner.collection; import java.util.ArrayList; import java.util.Collection; impo… Read More
  • How to check file existence in Java Following example shows how to check file existence using file.exists() method in Java. existspublic boolean exists() Tests whether the file or directory denoted by this abstract pathname exists. Returns:    … Read More
  • How to change File Last modified Date using Java Following example shows how to change file last modified date using file.setLastModified() method in Java. setLastModifiedpublic boolean setLastModified(long time)  Sets the last-modified time of the file or directory … Read More
  • How to convert a String to Lower caseFollowing example shows how to convert a string to lower case. Sample Programpackage com.javatutorialcorner.javastring;public class ToLowerCase { public static void main(String[] args) { String input = "Java tutoria… Read More
  • How to get the size of Collection Following example shows how to get the size of collection using collection.size() method in Java. int java.util.List.size()sizeint size() Returns the number of elements in this list. If this list contains more than Integer.… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to retrieve all the directory names from Directory using Java Rating: 5 Reviewed By: eHowToNow