First of all, I am really very sorry as I am unable to post the articles frequently. I'll try to post the tutorials or any technical articles atleast once in a week.
Try the above program to see the output.
As I was very busy with my work. I came across one situation of finding Sub-directories and Files in a folder. So I thought of sharing the logic of finding the files and folders inside any folder. This problem is very common and we can use File class of Java.
Here inside C:\Anjan, there are two files and one sub-folder/sub-directory. So the problem is to implement the same in Java. Let us see the program -
package com.anjan.demo;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
public class DirectoryDemo {
private static String[] findSubdirectory(String path) {
File file = new File(path);
String[] listDir = null;
if(file.isFile()) {
System.out.println("Oops! It is File. Please provide Directory");
} else if (file.isDirectory()) {
listDir = file.list(new FilenameFilter() {
@Override
public boolean accept(File curFile, String name) {
return new File(curFile, name).isDirectory();
}
});
}
return listDir;
}
private static String[] findFiles(String path) {
File file = new File(path);
String[] listDir = null;
if(file.isFile()) {
System.out.println("Oops! It is File. Please provide Directory");
} else if (file.isDirectory()) {
listFile = file.list(new FilenameFilter() {
@Override
public boolean accept(File curFile, String name) {
return new File(curFile, name).isFile();
}
});
}
return listFile;
}
public static void main(String args[]) {
String path = "C:\\Anjan";
String[] dir = findSubdirectory(path);
System.out.println("SubFolders : "+Arrays.toString(dir));
String[] files = findFiles(path);
System.out.println("Files in Directory : "+Arrays.toString(files));
}
}
import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
public class DirectoryDemo {
private static String[] findSubdirectory(String path) {
File file = new File(path);
String[] listDir = null;
if(file.isFile()) {
System.out.println("Oops! It is File. Please provide Directory");
} else if (file.isDirectory()) {
listDir = file.list(new FilenameFilter() {
@Override
public boolean accept(File curFile, String name) {
return new File(curFile, name).isDirectory();
}
});
}
return listDir;
}
private static String[] findFiles(String path) {
File file = new File(path);
String[] listDir = null;
if(file.isFile()) {
System.out.println("Oops! It is File. Please provide Directory");
} else if (file.isDirectory()) {
listFile = file.list(new FilenameFilter() {
@Override
public boolean accept(File curFile, String name) {
return new File(curFile, name).isFile();
}
});
}
return listFile;
}
public static void main(String args[]) {
String path = "C:\\Anjan";
String[] dir = findSubdirectory(path);
System.out.println("SubFolders : "+Arrays.toString(dir));
String[] files = findFiles(path);
System.out.println("Files in Directory : "+Arrays.toString(files));
}
}
Try the above program to see the output.