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
......
The following example shows printing directory hierarchy as a TREE just like DOS tree command.
http://bethecoder.com/applications/tutorials/showTutorials.action?tutorialId=Java_Core_General_FileMap
Good luck
if (subNote != null) {
for (String filename : subNote) {<—– null exception
displayIt(new File(node, filename));
}
}
Thanks a lot
wow. this is pretty straight-forward. I panicked after seeing this question for the first time. Thanks MKYong.
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
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.
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.
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
/* 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);
}
}
}
}
Awesome example MKYong… thanks for saving me some time! (^-^)