In this tutorial, we are going to learn how we can learn only specific files like .png or .jpg or any other file extension which are allocated inside folder and sub-folders.
Here, what we gonna do is to list all the folders and subfolders. After that looping through the folders to filter out and get the specific desired file from those folders.
Create a sample Java class called FolderFileReader.java
Create a method that lists all the subfolders present in the folder.
public static void listDirSubDir(String directoryName, List<String> directoryList) throws Exception {
File directory = new File(directoryName);
File[] fList = directory.listFiles();
if (fList != null)
for (File file : fList) {
if (file.isDirectory()) {
String folder = file.getAbsolutePath();
directoryList.add(folder);
listDirSubDir(folder, directoryList);
}
}
}
We are using recursion in order to list all the subfolders and add the available folders to the List called directoryList
Now, let's create a method that will list all the files present in the particular folder.
public static ArrayList<String> getFolderFiles(String folder) throws Exception {
ArrayList<String> FileList = new ArrayList<String>();
FileExtensionFilter filter = new FileExtensionFilter(extension);
File dir = new File(folder);
if (!dir.isDirectory()) {
throw new Exception("Directory does not exists: " + folder);
}
String[] list = dir.list(filter);
assert list != null;
if (list.length == 0) {
System.out.println("File not found in this folder : " + dir.getName());
}
for (String file : list) {
String temp = folder + File.separator + file;
FileList.add(temp);
}
return FileList;
}
The FileExtensionFilter is the filter class used to read specific file like .png or .jpg.
Let's create the filter class
public static class FileExtensionFilter implements FilenameFilter {
private String ext;
public FileExtensionFilter(String ext) {
this.ext = ext;
}
public boolean accept(File dir, String name) {
return name.endsWith(this.ext);
}
}
Now, implement the main method
public static void main(String[] args) {
String directory = "folder_path";
ArrayList<String> directoryList = new ArrayList<>();
try {
directoryList.add(directory); // adding current directory
listDirSubDir(directory, directoryList);
System.out.println("Total directory found: " + directoryList.size());
int totalFileFound = 0;
for (String folder : directoryList) {
ArrayList<String> files = getFolderFiles(folder);
for (String file : files) {
System.out.println(file);
}
totalFileFound += files.size();
}
System.out.println("Total files found: " + totalFileFound);
} catch (Exception e) {
e.printStackTrace();
}
}
Here, we are simply getting the list of folders available, looping through it, and getting the files present in each folder.
The overall implementation looks as below:
package io;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
public class FolderFileReader {
private static final String extension = ".dcm";
public static void main(String[] args) {
String directory = "folder_path";
ArrayList<String> directoryList = new ArrayList<>();
try {
directoryList.add(directory); // adding current directory
listDirSubDir(directory, directoryList);
System.out.println("Total directory found: " + directoryList.size());
int totalFileFound = 0;
for (String folder : directoryList) {
ArrayList<String> files = getFolderFiles(folder);
for (String file : files) {
System.out.println(file);
}
totalFileFound += files.size();
}
System.out.println("Total files found: " + totalFileFound);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void listDirSubDir(String directoryName, List<String> directoryList) throws Exception {
File directory = new File(directoryName);
File[] fList = directory.listFiles();
if (fList != null)
for (File file : fList) {
if (file.isDirectory()) {
String folder = file.getAbsolutePath();
directoryList.add(folder);
listDirSubDir(folder, directoryList);
}
}
}
public static ArrayList<String> getFolderFiles(String folder) throws Exception {
ArrayList<String> FileList = new ArrayList<String>();
FileExtensionFilter filter = new FileExtensionFilter(extension);
File dir = new File(folder);
if (!dir.isDirectory()) {
throw new Exception("Directory does not exists: " + folder);
}
String[] list = dir.list(filter);
assert list != null;
if (list.length == 0) {
System.out.println("File not found in this folder : " + dir.getName());
}
for (String file : list) {
String temp = folder + File.separator + file;
FileList.add(temp);
}
return FileList;
}
public static class FileExtensionFilter implements FilenameFilter {
private String ext;
public FileExtensionFilter(String ext) {
this.ext = ext;
}
public boolean accept(File dir, String name) {
return name.endsWith(this.ext);
}
}
}
0 comments:
New comments are not allowed.