Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program to get all files within a directory in Java

Tags:

java

I'm working on this program to get all the files in the directory. For some reason I am getting a NullPointerException on Line 16. I don't know why though since this is a template that seemed to work in class with our teacher. Thanks.

import java.util.*;
import java.io.*;

public class FindDirectories {
    public static void main(String[] args) { 
        if (args.length == 0) { 
            args = new String[] { ".." }; 
        }

        List<String> nextDir = new ArrayList<String>(); 
        nextDir.add(args[0]); // either the one file, or the directory
        try { 
            while(nextDir.size() > 0) {     // size() is num of elements in List 
                File pathName = new File(nextDir.get(0)); // gets the element at the index of the List 
                String[] fileNames = pathName.list();  // lists all files in the directory
                for(int i = 0; i < fileNames.length; i++) { 
                  File f = new File(pathName.getPath(), fileNames[i]); // getPath converts abstract path to path in String, 
                                                                    // constructor creates new File object with fileName name   
                  if (f.isDirectory()) { 
                     System.out.println(f.getCanonicalPath()); 
                     nextDir.add(f.getPath()); 
                  } 
                  else {
                      System.out.println(f);
                  }
               } 
               nextDir.remove(0); 
            } 
        } 
        catch(IOException e) { 
            e.printStackTrace();  
        }       
    } 
}
like image 573
Crystal Avatar asked Jul 26 '10 05:07

Crystal


People also ask

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.

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.


2 Answers

Check out the Javadoc for File.list(). Specifically:

Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

In your code pathName.list(); must be returning null so pathName does not represent a valid directory, or an IO error occurred trying to get a list of files from that directory.

like image 155
krock Avatar answered Oct 18 '22 15:10

krock


Use bellow snippet to get all the files from all the sub directories:

import java.io.File;

/**
 *
 * @author santoshk
 */
public class ListFiles {

     File mainFolder = new File("F:\\personal");
     public static void main(String[] args)
     {
         ListFiles lf = new ListFiles();
         lf.getFiles(lf.mainFolder);
     }
     public void getFiles(File f){
         File files[];
         if(f.isFile())
             System.out.println(f.getAbsolutePath());
         else{
             files = f.listFiles();
             for (int i = 0; i < files.length; i++) {
                 getFiles(files[i]);
             }
         }
     }
}
like image 45
santosh Avatar answered Oct 18 '22 14:10

santosh