ChatGPT解决这个技术问题 Extra ChatGPT

Getting the filenames of all files in a folder [duplicate]

This question already has answers here: How to read all files in a folder from Java? (33 answers) Closed 2 years ago.

I need to create a list with all names of the files in a folder.

For example, if I have:

000.jpg
012.jpg
013.jpg

I want to store them in a ArrayList with [000,012,013] as values.

What's the best way to do it in Java ?

PS: I'm on Mac OS X


A
Andrei Suvorkov

You could do it like that:

File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
  if (listOfFiles[i].isFile()) {
    System.out.println("File " + listOfFiles[i].getName());
  } else if (listOfFiles[i].isDirectory()) {
    System.out.println("Directory " + listOfFiles[i].getName());
  }
}

Do you want to only get JPEG files or all files?


Yup there isn't but dealing with extensions is dependent on the extensions present cos files maybe present without extensions too.
btw if you want the current folder you can use File folder = new File ("."); ... than the same. example new File("./"+your_file);
@RoflcoptrException One doubt. Does this listofFiles[i] points to the first file in the folder (last created), or the last file (first created) ?
How can get specify extension to get .jpg files?
If you want filter out only .JPG files and sort the result in oldest file first, you can use it like this: File dir = new File("/your/path"); FileFilter fileFilter = new WildcardFileFilter("*.JPG", IOCase.INSENSITIVE); // For taking both .JPG and .jpg files (useful in *nix env) File[] fileList = dir.listFiles(fileFilter); if (fileList.length > 0) { /** The oldest file comes first **/ Arrays.sort(fileList, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR); } //filesList now contains all the JPG/jpg files in sorted order
E
Eric Leschinski

Create a File object, passing the directory path to the constructor. Use the listFiles() to retrieve an array of File objects for each file in the directory, and then call the getName() method to get the filename.

List<String> results = new ArrayList<String>();


File[] files = new File("/path/to/the/directory").listFiles();
//If this pathname does not denote a directory, then listFiles() returns null. 

for (File file : files) {
    if (file.isFile()) {
        results.add(file.getName());
    }
}

You should actually call listFiles() ;)
@Progman Could be helpful. :)
You can put a filter on listFiles so as to only return files with a certain extension File[] files = new File("/path/to/the/directory").listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(".xml"); } });
File[] files = new File(fullPath).listFiles(); List names = Arrays.asList(files).parallelStream().map(file -> file.getName()).collect(Collectors.toList());
You can filter files using lambda expression (Java 8+): File[] files = new File("path/to/dir").listFiles((dir, name) -> name.endsWith(".xml"));
M
Muhd

Here's how to look in the documentation.

First, you're dealing with IO, so look in the java.io package.

There are two classes that look interesting: FileFilter and FileNameFilter. When I clicked on the first, it showed me that there was a a listFiles() method in the File class. And the documentation for that method says:

Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.

Scrolling up in the File JavaDoc, I see the constructors. And that's really all I need to be able to create a File instance and call listFiles() on it. Scrolling still further, I can see some information about how files are named in different operating systems.