
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - File listFiles() method
Description
The Java File listFiles() method returns the array of abstract pathnames defining the files in the directory denoted by this abstract pathname.
Declaration
Following is the declaration for java.io.File.listFiles() method −
public File[] listFiles()
Parameters
NA
Return Value
The method returns an array of pathnames for 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 listFiles() method
The following example shows the usage of Java File listFiles() method.
We've created a filter class by implementing FilenameFilter class. 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 listFiles() 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; File[] paths; try { // create new file f = new File("F:/test"); // array of files and directory paths = f.listFiles(); // for each file in the path array for(File path:paths) { // prints filename and directory name System.out.println(path.getName()); } } 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.pptx test.txt Test1.txt
Example - Usage of File listFiles() method
The following example shows the usage of Java File listFiles() 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 listFiles() method into a File array. Then this array is iterated to print the available file/directory names 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; File[] paths; try { // create new file f = new File("."); // array of files and directory paths = f.listFiles(); // for each file in the path array for(File path:paths) { // prints filename and directory name System.out.println(path.getName()); } } 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 listFiles() method
The following example shows the usage of Java File listFiles() 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 listFiles() method into a File 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; File[] paths; try { // create new file f = new File("F:/Test3"); // array of files and directory paths = f.listFiles(); if(paths != null) { // for each name in the path array for(File path:paths) { // prints filename and directory name System.out.println(path.getName()); } }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