Obiedog
Obiedog

Reputation: 424

How to get contents of a folder and put into an ArrayList

I want to use

File f = new File("C:\\");

to make an ArrayList with the contents of the folder.

I am not very good with buffered readers, so please tell me if that is better.

Here's the code I have so far:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;


public class buffered_read {
public static void main(String[] args) {
    File f = new File("C:\\");
    int x = 0;
    boolean b = true;
    File list[];
    while(b = true){

    }
}
}

Thanks, obiedog

Upvotes: 38

Views: 90631

Answers (5)

Vishal
Vishal

Reputation: 724

If you want to retrieve all the files recursively, you can do something like below

public class FileLister {

    static List<String> fileList = new ArrayList<String>();

    public static void main(String[] args) {
        File file = new File("C:\\tmp");
        listDirectory(file);
        System.out.println(fileList);
    }

    public static void listDirectory(File file) {
        if(file.isDirectory()) {
            File[] files = file.listFiles();
            for(File currFile : files) {
                listDirectory(currFile);
            }
        }
        else {
            fileList.add(file.getPath());
        }
    }
}

Upvotes: 0

Kamran
Kamran

Reputation: 853

Streams are fast and very easy to read:

final Path rootPath = Paths.get("/tmp");

// All entries Path objects
ArrayList<Path> all = Files
        .list(rootPath)
        .collect(Collectors.toCollection(ArrayList::new));

// Only regular files at Path objects
ArrayList<Path> regularFilePaths = Files
        .list(rootPath)
        .filter(Files::isRegularFile)
        .collect(Collectors.toCollection(ArrayList::new));

// Only regular files as String paths
ArrayList<String> regularPathsAsString = Files
        .list(rootPath)
        .filter(Files::isRegularFile)
        .map(Path::toString)
        .collect(Collectors.toCollection(ArrayList::new));

Upvotes: 3

user927911
user927911

Reputation: 786

The easiest way of doing that is:

File f = new File("C:\\");
ArrayList<File> files = new ArrayList<File>(Arrays.asList(f.listFiles()));

And if what you want is a list of names:

File f = new File("C:\\");
ArrayList<String> names = new ArrayList<String>(Arrays.asList(f.list()));

Upvotes: 77

Lukas Knuth
Lukas Knuth

Reputation: 25755

The File-class offers a listFiles()-method which returns a File-array of all files in the current folder.

To make an ArrayList of them, you can use the Arrays-class and it's asList()-method. See here.

If you only need the file-names or paths as Strings, there is also a list()-method which returns a String-array. To convert the array to an ArrayList, follow the steps illustrated in the linked question.

Upvotes: 4

Philipp Reichart
Philipp Reichart

Reputation: 20961

Have you read the API documentation for java.io.File?

File f = new File("C:\\");
File[] list = f.listFiles();

Upvotes: 30

Related Questions