Arsenal IL Staff
Arsenal IL Staff

Reputation: 1

get file names by directory and sub directory

i need to get all file names by giving a directory and sub directories from some reason it doesn't work and i have no idea what is the problem

this is what i did:

  import java.io.File;

public class Main
{

    public static void main(String[] args) {
        String root = "C:\\eclipse";// root directory
        String[] filename = null;

        filename = getFfileNamesAndInsertToArray(root, filename);
        printFileNames(filename);
    }

    public static void printFileNames(String[] filenames) {

        for (int i = 0; i < filenames.length; i++) {
            System.out.println(filenames[i]);
        }
    }

    public static String[] getFfileNamesAndInsertToArray(String root, String[] filenames) {

        String[] files = filenames;
        java.io.File dir = new java.io.File(root);
        for (java.io.File file : dir.listFiles()) {
            // String path = file.getAbsolutePath(); // get path of file
            if (file.isDirectory() == false) {
                files[files.length + 1] = file.getName();
            }

            else {
                root = file.getAbsolutePath();
                getFfileNamesAndInsertToArray(root, files);
            }

        }
        return files;
    }

}

Upvotes: 0

Views: 430

Answers (3)

AppX
AppX

Reputation: 528

Could do something like this:

public static List<String> getListOfFiles(String root) throws IOException {
    final List<String> fileNames = new ArrayList<>();
    Files.walkFileTree(Paths.get(root), new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (attrs.isRegularFile()) {
                fileNames.add(file.getFileName().toString());
            }
            return super.visitFile(file, attrs);
        }
    });
    return fileNames;
}

Upvotes: 0

Mofodev
Mofodev

Reputation: 1

You have to initialise any array with a specific size like:

String[] files = new String[20];

So if you don't know the size in particular (like in your case), you could use an ArrayList. If you do not initialise your string-array and want to save a string at position length+1, your program will fail since your array has no length at that point. And it does not dynamically allocate new memory, hence it cannot grow and get larger. If you use an ArrayList of type String you cann call the arraylist.add-method to add new Elements and the list gets appended everytime, so you don't have to worry about it's size.

Upvotes: 0

micha
micha

Reputation: 49612

You don't have to do this by hand. You can use Files.walkFileTree for this

Path path = Paths.get("your/directory/path");
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
     @Override
     public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
       // this is called for each file
     }
 });

Upvotes: 2

Related Questions