Java.io.File.list() Method
Advertisements
Description
The java.io.File.list() 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
The following example shows the usage of java.io.File.list() method.
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("c:/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();
}
}
}
Let us compile and run the above program, this will produce the following result:
child_test child_test.txt