Home » Java programming language

Java File Class String[] list() method with Example

Java File Class String[] list() method: Here, we are going to learn about the String[] list() method of File class with its syntax and example.
Submitted by Preeti Jain, on July 08, 2019

File Class String[] list()

  • This method is available in package java.io.File.list().
  • This method is used to return the names of all the files in the form of an array of strings which is represented in the filepath.
  • The return type of this method is String[] i.e. It returns an array of strings of all the files which is represented in filepath if the given path is directory else return null.
  • This method may raise an exception( i.e. Security Exception) if the write access is not given to the file.
  • This method is overridable. The first method does not accept any parameter and the second method accept one parameter.


    String[] list(){


We don't pass any object as a parameter in the method of the File.

Return value:

The return type of this method is String[] i.e. it returns all files names in an array of strings which is represented in a filepath.

Java program to demonstrate example of list() method

// import the File class because we will use File class methods
import java.io.File;

// import the Exception class because it may raise an 
// exception when working with files
import java.lang.Exception;

public class ToListFiles {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes to 
            // escape '\' character sequence for windows otherwise 
            // it will be considerable as url.
            File file = new File("C:\\Users\\computer clinic\\OneDrive\\Articles");

            // By using list() returns all the files and directories 
            // which is represented in a file path if file path is a directory .
            String[] filelist = file.list();
            System.out.println("These are the name of files represented in a given directory :" + file.getPath());

            // By using loop to traverse the filenames and directories 
            // in the given path .
            for (int i = 0; i < filelist.length; i++)
        } catch (Exception e) {
            System.out.println("An error occurred.");


D:\Programs>javac ToListFiles.java

D:\Programs>java ToListFiles
These are the name of files represented in a given directory : C:\Users\computer clinic\OneDrive\Articles
Java LinkedList addFirst.docx
Types of Inheritance in Java.docx

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.