Languages
[Edit]
EN

Java - get list of files and directories from directory

0 points
Created by:
Dirask Community
6300

In this article, we would like to show you how to get the list of files and directories from the directory specified by its path in Java.

Quick solution:

File myFile = new File("C:\\some_path\\example");
String[] fileList = myFile.list(); // returns array of files and directories

 

Practical example

In this example, we use Java.if.File.list() method to get the list of files and directories under the given path.

import java.io.File;
import java.io.IOException;
import java.util.Objects;

public class Example {

    public static void main(String[] args) {
        File myFile = new File("C:\\projects\\example");
        String[] paths = myFile.list(); // array of files and directories

        String[] strings = Objects.requireNonNull(paths); // creates strings if paths != null

        for (String path : strings) {
            System.out.println(path);
        }
    }
}

References

Java - file operations

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join