Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list all files from directories and subdirectories in Java

Tags:

java

directory

What would be the fastest way to list the names of files from 1000+ directories and sub-directories?

EDIT; The current code I use is:

import java.io.File;  public class DirectoryReader {    static int spc_count=-1;    static void Process(File aFile) {     spc_count++;     String spcs = "";     for (int i = 0; i < spc_count; i++)       spcs += " ";     if(aFile.isFile())       System.out.println(spcs + "[FILE] " + aFile.getName());     else if (aFile.isDirectory()) {       System.out.println(spcs + "[DIR] " + aFile.getName());       File[] listOfFiles = aFile.listFiles();       if(listOfFiles!=null) {         for (int i = 0; i < listOfFiles.length; i++)           Process(listOfFiles[i]);       } else {         System.out.println(spcs + " [ACCESS DENIED]");       }     }     spc_count--;   }    public static void main(String[] args) {     String nam = "D:/";     File aFile = new File(nam);     Process(aFile);   }  } 
like image 414
Adnan Avatar asked Jun 09 '10 16:06

Adnan


People also ask

How do you get a list of all files in a directory and its subdirectories in Java?

File. list() The simplest method to list the names of files and folders in a given directory, without traversing the sub-directories, is the helper method . list() , which returns an array of String s.

How do you get the list of all files in a folder in Java?

The List() method. This method returns a String array which contains the names of all the files and directories in the path represented by the current (File) object. Using this method, you can just print the names of the files and directories.

How do you find subdirectories in Java?

You can use String[] directories = file. list() to list all file names, then use loop to check each sub-files and use file. isDirectory() function to get subdirectories.

What does the files list () method in Java do?

list() returns the array of files and directories in the directory defined by this abstract path name. The method returns null, if the abstract pathname does not denote a directory.


1 Answers

As this answer shows up on top of google, i'm adding a java 7 nio solution for listing all files and directories, it is takes about 80% less time on my system.

try {     Path startPath = Paths.get("c:/");     Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {         @Override         public FileVisitResult preVisitDirectory(Path dir,                 BasicFileAttributes attrs) {             System.out.println("Dir: " + dir.toString());             return FileVisitResult.CONTINUE;         }          @Override         public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {             System.out.println("File: " + file.toString());                 return FileVisitResult.CONTINUE;         }          @Override         public FileVisitResult visitFileFailed(Path file, IOException e) {             return FileVisitResult.CONTINUE;         }     }); } catch (IOException e) {     e.printStackTrace(); } 
like image 199
Aksel Willgert Avatar answered Oct 01 '22 10:10

Aksel Willgert