Java File list() Method
File.list() is a method in Java's java.io.File an instance method provided by the class, used to obtain the list of names of all files and subdirectories in a specified directory. This method is very useful for scenarios that need to traverse or process directory contents.
Method Syntax
public
String
[
]
list
(
)
Method Return Value
list() The method returns a string array (String[]), containing the following:
- If the calling object represents a valid directory, returns the names of all files and subdirectories in that directory
- If the calling object does not represent a directory or an I/O error occurs, returns
null - If the directory is empty, returns an empty array
Basic Usage Example
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:/MyFolder"
)
;
// Get the list of files and subdirectories in the directory
String
[
]
files
=
directory.
list
(
)
;
// Check whether the list was obtained successfully
if
(
files
!=
null
)
{
System
.
out
.
println
(
"Directory contents:"
)
;
for
(
String
file
:
files
)
{
System
.
out
.
println
(
file
)
;
}
}
else
{
System
.
out
.
println
(
"The specified path is not a directory or an error occurred"
)
;
}
}
}
Method overloads
File The class also provides another version of list() method that can accept a filter:
Example
public
String
[
]
list
(
FilenameFilter
filter
)
The FilenameFilter interface
FilenameFilter is a functional interface containing only one method:
Example
boolean
accept
(
File
dir,
String
name
)
Example using a filter
Example
import
java.io.File
;
import
java.io.FilenameFilter
;
public
class
FilteredListExample
{
public
static
void
main
(
String
[
]
args
)
{
File
directory
=
new
File
(
"C:/MyFolder"
)
;
// List only .txt files
String
[
]
textFiles
=
directory.
list
(
new
FilenameFilter
(
)
{
@Override
public
boolean
accept
(
File
dir,
String
name
)
{
return
name.
endsWith
(
".txt"
)
;
}
}
)
;
// Java 8+ can use a Lambda expression shorthand
String
[
]
textFiles
=
directory.
list
(
(
dir, name
)
->
name.
endsWith
(
".txt"
)
)
;
if
(
textFiles
!=
null
)
{
System
.
out
.
println
(
"Text file list:"
)
;
for
(
String
file
:
textFiles
)
{
System
.
out
.
println
(
file
)
;
}
}
}
}
Notes
- Permission issues: if the program does not have permission to read the directory,
list()the method returnsnull - Symbolic links:
list()method follows symbolic links and may return the actual file/directory names the links point to - Performance considerations: for directories containing a large number of files,
list()method may consume more memory - Relative path: the returned file names contain only the name part, not the complete path
Alternative methods
In Java 7 and above, it is recommended to use Files class and DirectoryStream to handle directory contents, which provide more modern and flexible ways:
Example
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
public
class
NioListExample
{
public
static
void
main
(
String
[
]
args
)
throws
IOException
{
Path dir
=
Paths.
get
(
"C:/MyFolder"
)
;
try
(
DirectoryStream
<
Path
stream
=Files.
newDirectoryStream
(
dir
)
)
{
for
(
Path file
:
stream
)
{
System
.
out
.
println
(
file.
getFileName
(
)
)
;
}
}
}
}
Summary
File.list() The method is a simple and direct way to get directory contents, suitable for simple file system operations. For more complex needs, you can consider using FilenameFilter or turn to the more powerful features provided by the NIO.2 API.
Java File