AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-file-handling: How to find all the files in a directory and its sub-directories?

I want to get all the file names located in a folder and its all sub folders too.

corejava x 348
file-handling x 8
Posted On : 2017-07-22 17:14:56.0
profile Saksham Kumar - anyforum.in Saksham Kumar
72933915558
up-rate
5
down-rate

Answers


Please check below code, with the use of recursion, you can check all the file names located in a folder and its all sub folders.


FindFiles.java:
-----------------------------------------
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FindFiles {
public static void main(String s1[]){
File folder = new File("E:\\SONGS\\AUDIO\\BOLLYWOOD\\fvrt\\New APR 2014");
List<String> files=new ArrayList<String>();
setFilesInList(folder, files);
for(String fileName:files){
System.out.println(fileName.contains("jpg")?fileName:"");
}

}

public static void setFilesInList(File folder, List<String> files){
File[] listOfFiles = folder.listFiles();
if(null!=listOfFiles){
for (int i = 0; i < listOfFiles.length; i++) {
if( listOfFiles[i].canRead()){
if (listOfFiles[i].isFile()) {
files.add(listOfFiles[i].getName());
System.out.println("File Name:" + listOfFiles[i].getName()+" and File Size in bytes:"+listOfFiles[i].getTotalSpace());
} else if (listOfFiles[i].isDirectory()) {
setFilesInList(listOfFiles[i],files);
System.out.println("Sub-Directory Name:" + listOfFiles[i].getName()+" and Sub-Directory Size in bytes:"+listOfFiles[i].getTotalSpace());
}
}
}
}
}
}

Posted On : 2017-07-22 17:18:06
Satisfied : 0 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523186220845
Reply This Thread
up-rate
0
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in