Java File listFiles() Method
listFiles() The method is in Java's java.io.File an important method provided by the class, used to obtain all files and subdirectories in the current directory. The method returns a File object array, each File object representing a file or subdirectory in the directory.
Method Syntax
listFiles() The method has the following overloads:
public
File
[
]
listFiles
(
)
public
File
[
]
listFiles
(
FilenameFilter
filter
)
public
File
[
]
listFiles
(
FileFilter
filter
)
Basic Usage
Get all files and subdirectories in the directory
The most basic usage is to call directly without any parameters listFiles() method in the component:
Example
import
java.io.File
;
public
class
ListFilesExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create a File object representing the directory
File
directory
=
new
File
(
"C:/example"
)
;
// Get all files and subdirectories in the directory
File
[
]
files
=
directory.
listFiles
(
)
;
// Traverse and print file names
if
(
files
!=
null
)
{
for
(
File
file
:
files
)
{
System
.
out
.
println
(
file.
getName
(
)
)
;
}
}
}
}
Method parameters
The FilenameFilter interface
FilenameFilter is a functional interface used to filter file names:
Example
File
[
]
listFiles
(
FilenameFilter
filter
)
Example: list only .txt file
Example
File
directory
=
new
File
(
"C:/example"
)
;
File
[
]
textFiles
=
directory.
listFiles
(
(
dir, name
)
->
name.
endsWith
(
".txt"
)
)
;
The FileFilter interface
FileFilter is also a functional interface, but it is based on File objects for filtering:
Example
File
[
]
listFiles
(
FileFilter
filter
)
Example: list only directories
Example
File
directory
=
new
File
(
"C:/example"
)
;
File
[
]
subDirectories
=
directory.
listFiles
(
File
::
isDirectory
)
;
Notes
-
Null check: If
FileIf the object is not a directory or has no read permission,listFiles()returnsnull, so you need to check the return value. -
Performance considerations: for directories containing a large number of files,
listFiles()may consume more memory because it loads all file information at once. -
Sorting issues: the returned file array is not guaranteed to be in any particular order; if sorting is needed, you can sort the array manually.
-
Symbolic links: in Unix/Linux systems,
listFiles()follows symbolic links.
Practical Application Examples
Recursively list all files
Example
import
java.io.File
;
public
class
RecursiveFileListing
{
public
static
void
listFilesRecursively
(
File
directory
)
{
File
[
]
files
=
directory.
listFiles
(
)
;
if
(
files
!=
null
)
{
for
(
File
file
:
files
)
{
if
(
file.
isDirectory
(
)
)
{
listFilesRecursively
(
file
)
;
}
else
{
System
.
out
.
println
(
file.
getAbsolutePath
(
)
)
;
}
}
}
}
public
static
void
main
(
String
[
]
args
)
{
listFilesRecursively
(
new
File
(
"C:/example"
)
)
;
}
}
Using the Java 8 Stream API
Example
import
java.io.File
;
import
java.util.Arrays
;
import
java.util.stream.Stream
;
public
class
FileStreamExample
{
public
static
void
main
(
String
[
]
args
)
{
File
directory
=
new
File
(
"C:/example"
)
;
// Process files using Stream
Stream.
of
(
directory.
listFiles
(
)
)
.
filter
(
File
::
isFile
)
.
map
(
File
::
getName
)
.
forEach
(
System
.
out
::
println
)
;
}
}
Alternatives
In Java 7 and above, you can consider using java.nio.file in the package Files and Path class, which provides more modern file operation ways:
Example
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.util.stream.Stream
;
public
class
NioFileListing
{
public
static
void
main
(
String
[
]
args
)
throws
Exception
{
Path dir
=
Paths.
get
(
"C:/example"
)
;
try
(
Stream
<
Path
stream
=Files.
list
(
dir
)
)
{
stream.
filter
(
Files
::
isRegularFile
)
.
map
(
Path
::
getFileName
)
.
forEach
(
System
.
out
::
println
)
;
}
}
}
Summary
listFiles() The method is a basic tool in Java file operations; mastering its use is very important for file handling. In actual development, choose the appropriate filtering method based on specific needs and pay attention to handling possible exceptional cases. For new projects, it is recommended to consider using Java NIO's Files class, which provides more powerful and flexible file operation features.
Java File