Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java File list() Method

Java File list() Method

Java File Java File


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:


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

  1. Permission issues: if the program does not have permission to read the directory,list() the method returns null
  2. Symbolic links:list() method follows symbolic links and may return the actual file/directory names the links point to
  3. Performance considerations: for directories containing a large number of files,list() method may consume more memory
  4. 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 Java File