How to traverse a directory structure in Java

In this example, the program will traverse the given directory and print out all the directories and files absolute path and name one by one.

Example


package com.mkyong.io;

import java.io.File;

public class DisplayDirectoryAndFile{

	public static void main (String args[]) {

		displayIt(new File("C:\\Downloads"));
	}
	
	public static void displayIt(File node){
		
		System.out.println(node.getAbsoluteFile());
		
		if(node.isDirectory()){
			String[] subNote = node.list();
			for(String filename : subNote){
				displayIt(new File(node, filename));
			}
		}
		
	}
}

Output


C:\Downloads
C:\Downloads\100 Java Tips.pdf
C:\Downloads\1590599799.rar
C:\Downloads\2009
C:\Downloads\573440.flv
C:\Downloads\575492.flv
C:\Downloads\avira_antivir_personal_en.exe
C:\Downloads\backup-mkyong.com-12-24-2009.tar.gz
......

10 comments on “How to traverse a directory structure in Java

  1. if (subNote != null) {
    for (String filename : subNote) {<—– null exception
    displayIt(new File(node, filename));
    }
    }

    Reply
  2. wow. this is pretty straight-forward. I panicked after seeing this question for the first time. Thanks MKYong.

    Reply
  3. Hi Mkyong,
    your code threw a NullPointerException at least on Windows Vista if a File/Directory is a Symbolic Link
    for example:
    “C:UsersnixArtDocumentsMy Videos” ,which actually points on my HD to
    “C:UsersnixArtVideos”

    so now a new question arose: how would you detect & handle symbolic links here?

    regardless, great work on your entire site!!!
    – nik

    Reply
  4. hi,
    i have the structure like below
    src
    packagename1
    com.one.two
    testpackage3
    there are three package names in src like i listed above
    i want to get the package names from src folder irrespective their structure.

    Reply
  5. Hey Mkyong,
    you are great. Please help me with the following problem:
    There are about 15 folders that I have. Every folder has a similar sub structure. I was to merge the contents of the folders:
    1. taking 2 folders at a time
    2. taking 3 folders at a time
    Such that both files with same name are saved.

    Example:
    folder 1
    —pqr
    ——abc.txt
    ——xyz.txt

    folder 2
    —pqr
    ——abc.txt

    after merging:
    folder 3
    —pqr
    ——-abc.txt
    ——-abc.txt(2)
    ——-xyz.txt

    please help me with a java code.

    Reply
  6. Dear Sir,

    i have an application that reads files from folder. Files can be multiple.And another application continusily writes the files to that folder. I want to read files one by one. But my application only reads the latest file at one time i dnt want this plzzz help…

    rgs,
    sanjay

    Reply
  7. /* Code for comparing two folder structure for there differences */
    package com.ex;

    import java.io.File;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;

    public class JCompare {
    static Set S1, S2;
    static File f1 = new File(“C:\\Users\\ravi2307\\Downloads”);
    static File f2 = new File(“C:\\Users\\ravi2307\\sdd”);

    public static void main(String args[]) {
    S1 = new HashSet();
    S2 = new HashSet();
    compare(f1, f2);
    }

    public static void compare(File f1, File f2) {
    displayIt(f1, S1);
    displayIt(f2, S2);
    String temp = null;
    System.out.println(“\n\n\n=============DIFFERENCES===============”);

    Iterator itr = S2.iterator();
    while (itr.hasNext()) {
    temp = itr.next();
    if (!S1.contains(temp)) {
    System.out.println(“Folder1: “+temp);
    }
    }

    Iterator itr1 = S1.iterator();
    while (itr1.hasNext()) {
    temp = itr1.next();
    if (!S2.contains(temp)) {
    System.out.println(“Folder2: “+temp);
    }
    }

    }

    public static void displayIt(File node, Set s1) {
    if (node.getAbsoluteFile().toString()
    .contains(f1.getAbsolutePath().toString()))
    s1.add(node.getAbsoluteFile().toString()
    .replace(f1.getAbsolutePath().toString(), “”));
    else {
    s1.add(node.getAbsoluteFile().toString()
    .replace(f2.getAbsolutePath().toString(), “”));
    }
    System.out.println(node.getAbsoluteFile().toString());

    if (node.isDirectory()) {
    String[] subNote = node.list();
    for (String filename : subNote) {
    displayIt(new File(node, filename), s1);

    }
    }
    }
    }

    Reply
  8. Awesome example MKYong… thanks for saving me some time! (^-^)

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *