获取文件夹中所有文件的文件名[duplicate]

时间:2022-02-23 00:35:26

Possible Duplicate:
Read all files in a folder

可能重复:读取文件夹中的所有文件。

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.

我想将它们存储在一个ArrayList中,以[000,012,013]作为值。

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

在Java中最好的方法是什么?

PS: I'm on Mac OS X

PS:我用的是Mac OS X

3 个解决方案

#1


600  

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?

您希望只获取JPEG文件或所有文件吗?

#2


106  

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.

创建一个文件对象,将目录路径传递给构造函数。使用listFiles()检索目录中每个文件的文件对象数组,然后调用getName()方法获取文件名。

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());
    }
}

#3


54  

Here's how to look in the documentation.

下面是如何查看文档。

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

首先,您正在处理IO,所以请查看java。io包。

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:

有两个类看起来很有趣:FileFilter和FileNameFilter。当我单击第一个时,它显示在File类中有一个listFiles()方法。这个方法的文档说

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.

在JavaDoc文件中向上滚动,我看到了构造函数。这就是我创建文件实例和调用listFiles()的全部需要。继续滚动,我可以看到关于在不同操作系统中如何命名文件的一些信息。

#1


600  

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?

您希望只获取JPEG文件或所有文件吗?

#2


106  

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.

创建一个文件对象,将目录路径传递给构造函数。使用listFiles()检索目录中每个文件的文件对象数组,然后调用getName()方法获取文件名。

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());
    }
}

#3


54  

Here's how to look in the documentation.

下面是如何查看文档。

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

首先,您正在处理IO,所以请查看java。io包。

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:

有两个类看起来很有趣:FileFilter和FileNameFilter。当我单击第一个时,它显示在File类中有一个listFiles()方法。这个方法的文档说

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.

在JavaDoc文件中向上滚动,我看到了构造函数。这就是我创建文件实例和调用listFiles()的全部需要。继续滚动,我可以看到关于在不同操作系统中如何命名文件的一些信息。