38

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

James Dunn
  • 8,064
  • 13
  • 53
  • 87
Obiedog
  • 424
  • 1
  • 4
  • 7
  • 1
    possible duplicate of [how to get the file and folder information in java?](http://stackoverflow.com/questions/5026954/how-to-get-the-file-and-folder-information-in-java) –  Sep 04 '11 at 19:59
  • 2
    Sounds a bit like homework... Try using File.list(). – Janick Bernet Sep 04 '11 at 20:00

5 Answers5

77

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()));
user927911
  • 786
  • 6
  • 2
  • 4
    Does the File API automatically recurse into child folders? – blong Mar 19 '14 at 14:15
  • 1
    @blong No, it doesn't. `File.listFiles()` only lists the content of the given directory. For recursive listing see http://stackoverflow.com/a/14676464/1340631 or http://stackoverflow.com/a/2534643/1340631. – scai Apr 15 '15 at 09:40
  • Strange, `Paths.get(p.getParent()).listFiles()` and `File parentFile = p.toFile().getParentFile().listFiles()` both return null. – Sridhar Sarnobat Apr 04 '22 at 00:07
30

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

File f = new File("C:\\");
File[] list = f.listFiles();
Philipp Reichart
  • 20,771
  • 6
  • 58
  • 65
4

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.

Community
  • 1
  • 1
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
3

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));
Kamran
  • 843
  • 1
  • 8
  • 19
0

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());
        }
    }
}
Vishal
  • 674
  • 1
  • 7
  • 20