DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • How To Build a Google Photos Clone - Part 1
  • Configuring Java Apps With Kubernetes ConfigMaps and Helm
  • Java High Availability With WildFly on Kubernetes
  • How to Install and Configure Apache2

Trending

  • Setting up Request Rate Limiting With NGINX Ingress
  • Using Open Source for Data Integration and Automated Synchronizations
  • Understanding Git
  • How To Start a Successful Career in DevOps
  1. DZone
  2. Coding
  3. Java
  4. Java Example: List All Files From a Directory (or Subdirectory)

Java Example: List All Files From a Directory (or Subdirectory)

This is a basic algorithm, but it can be very useful in some situations and very handy for those that are learning Java.

Loiane Groner user avatar by
Loiane Groner
·
Jun. 26, 12 · Code Snippet
Like (2)
Save
Tweet
Share
320.38K Views

Join the DZone community and get the full member experience.

Join For Free

This is a basic algorithm, but it can be very useful in some situations and very handy for those that are learning Java.

The class bellow contains 4 methods:

  1. the first one will list all the files and folder names under a directory
  2. the second one will list all the file names under a directory
  3. the third one will list all the folder names under a directory
  4. the fourth one will list all the files from the directory and its sub-directories (be carefull with this one!)

Following is the code:

package com.loiane.util;

import java.io.File;

/**
 * Contains some methods to list files and folders from a directory
 *
 * @author Loiane Groner
 * http://loiane.com (Portuguese)
 * http://loianegroner.com (English)
 */
public class ListFilesUtil {

    /**
     * List all the files and folders from a directory
     * @param directoryName to be listed
     */
    public void listFilesAndFolders(String directoryName){

        File directory = new File(directoryName);

        //get all the files from a directory
        File[] fList = directory.listFiles();

        for (File file : fList){
            System.out.println(file.getName());
        }
    }

    /**
     * List all the files under a directory
     * @param directoryName to be listed
     */
    public void listFiles(String directoryName){

        File directory = new File(directoryName);

        //get all the files from a directory
        File[] fList = directory.listFiles();

        for (File file : fList){
            if (file.isFile()){
                System.out.println(file.getName());
            }
        }
    }

    /**
     * List all the folder under a directory
     * @param directoryName to be listed
     */
    public void listFolders(String directoryName){

        File directory = new File(directoryName);

        //get all the files from a directory
        File[] fList = directory.listFiles();

        for (File file : fList){
            if (file.isDirectory()){
                System.out.println(file.getName());
            }
        }
    }

    /**
     * List all files from a directory and its subdirectories
     * @param directoryName to be listed
     */
    public void listFilesAndFilesSubDirectories(String directoryName){

        File directory = new File(directoryName);

        //get all the files from a directory
        File[] fList = directory.listFiles();

        for (File file : fList){
            if (file.isFile()){
                System.out.println(file.getAbsolutePath());
            } else if (file.isDirectory()){
                listFilesAndFilesSubDirectories(file.getAbsolutePath());
            }
        }
    }

    public static void main (String[] args){

        ListFilesUtil listFilesUtil = new ListFilesUtil();

        final String directoryLinuxMac ="/Users/loiane/test";

        //Windows directory example
        final String directoryWindows ="C://test";

        listFilesUtil.listFiles(directoryLinuxMac);
    }
}
Directory Java (programming language)

Published at DZone with permission of Loiane Groner, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Build a Google Photos Clone - Part 1
  • Configuring Java Apps With Kubernetes ConfigMaps and Helm
  • Java High Availability With WildFly on Kubernetes
  • How to Install and Configure Apache2

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: