Java - File list() method



Description

The Java File list() method returns the array of files and directories in the directory defined by this abstract path name. The method returns null, if the abstract pathname does not denote a directory.

Declaration

Following is the declaration for java.io.File.list() method −

 public String[] list() 

Parameters

NA

Return Value

The method returns array of files and directories in the directory denoted by this abstract pathname.

Exception

  • SecurityException − If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file

Example - Usage of File list() method

The following example shows the usage of Java File list() method. We've created a File reference. Then we're creating a File Object using test directory which is present in the provided directory. Then we're getting the list of files present in the test directory using list() method into a string array. Then this array is iterated to print the available files in the given directory.

FileDemo.java

 package com.tutorialspoint; import java.io.File; public class FileDemo { public static void main(String[] args) { File f = null; String[] paths; try { // create new file f = new File("F:/test"); // array of files and directory paths = f.list(); // for each name in the path array for(String path:paths) { // prints filename and directory name System.out.println(path); } } catch(Exception e) { // if any error occurs e.printStackTrace(); } } } 

Output

Let us compile and run the above program, this will produce the following result−

 test.txt Test1.txt 

Example - Usage of File list() method

The following example shows the usage of Java File list() method. We've created a File reference. Then we're creating a File Object using current directory where we're running this program using ".". Then we're getting the list of files present in the current directory using list() method into a string array. Then this array is iterated to print the available files in the given directory.

FileDemo.java

 package com.tutorialspoint; import java.io.File; public class FileDemo { public static void main(String[] args) { File f = null; String[] paths; try { // create new file f = new File("."); // array of files and directory paths = f.list(); // for each name in the path array for(String path:paths) { // prints filename and directory name System.out.println(path); } } catch(Exception e) { // if any error occurs e.printStackTrace(); } } } 

Output

Let us compile and run the above program, this will produce the following result−

 .classpath .project .settings app.log bin properties.txt properties.xml src test.txt 

Example - Usage of File list() method

The following example shows the usage of Java File list() method. We've created a File reference. Then we're creating a File Object using test3 directory which is not present in the provided directory. Then we're getting the list of files present in the test directory using list() method into a string array. Then this array is iterated to print the available files in the given directory. As folder is not present, array will be null and we're printing a message - "Files are not present".

FileDemo.java

 package com.tutorialspoint; import java.io.File; public class FileDemo { public static void main(String[] args) { File f = null; String[] paths; try { // create new file f = new File("F:/Test3"); // array of files and directory paths = f.list(); if(paths != null) { // for each name in the path array for(String path:paths) { // prints filename and directory name System.out.println(path); } }else { System.out.println("Files are not present"); } } catch(Exception e) { // if any error occurs e.printStackTrace(); } } } 

Output

Let us compile and run the above program, this will produce the following result−

 Files are not present 
java_io_file_methods.htm
Advertisements