Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- /**
- *
- * @author Aamir khan
- */
- public class EbookShop {
- private static List<File> FinalData;
- public List<File> allFilesAndFolders, SubFolders;
- private final File ROOT = new File("PATH TO THE ROOT FOLDER");
- public EbookShop() {
- EbookShop.FinalData = new ArrayList();
- this.allFilesAndFolders = new ArrayList();
- this.SubFolders = new ArrayList();
- }
- //Fire Up
- public void Fire() {
- //get All Files and Folders
- allFilesAndFolders.addAll(Arrays.asList(ROOT.listFiles()));
- //Extract Folders
- allFilesAndFolders.iterator().forEachRemaining(x -> {
- try {
- if (x.isDirectory()) {
- SearchFiles(x);
- if (hasSubFolders(x)) {
- getSubFolders(x);
- }
- }else{
- //Take the Files out
- CheckAndAdd(x);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- });
- //Get Files From SubFolders
- SubFolders.iterator().forEachRemaining(y -> SearchFiles(y));
- }
- public final File[] listFolders(File path) {
- File[] listFiles = null;
- try {
- //list the Folders only those have Folders/Files inside
- listFiles = path.listFiles((File pathname)
- -> {
- return pathname.isDirectory()//make sure its Directory
- && !(pathname.isHidden())//Do not include any hidden File/Folder
- //Make Sure its not Empty
- && pathname.getAbsoluteFile().listFiles(
- (File pathname1) -> pathname1.isDirectory() || pathname1.isFile()).length != 0;
- }
- );
- } catch (Exception e) {
- e.printStackTrace();
- }
- return listFiles;
- }
- public final void SearchFiles(File folder) {
- if (folder.isDirectory()) {
- File[] listFiles = folder.listFiles(path -> CheckForExtension(path));
- for (File temp : listFiles) {
- CheckAndAdd(temp);
- }
- }
- }
- private boolean CheckAndAdd(File folder) {
- boolean flag = false;
- if (CheckForExtension(folder)) {
- flag = FinalData.add(folder);
- }
- return flag;
- }
- private boolean CheckForExtension(File file) {
- return (file.getName().toLowerCase().endsWith(".pdf"));
- // || file.getName().toLowerCase().endsWith(".7z")
- // || file.getName().toLowerCase().endsWith(".zip")
- // || file.getName().toLowerCase().endsWith(".rar"));
- }
- private boolean hasSubFolders(File x) {
- File[] listFolders = listFolders(x);
- return (listFolders.length != 0);
- }
- private void getSubFolders(File x) {
- File[] listFolders = listFolders(x);
- SubFolders.addAll(Arrays.asList(listFolders));
- }
- public static void main(String[] args)throws java.io.IOException {
- new EbookShop().Fire();
- //get the Final Result
- System.out.println("Total Files Found "+FinalData.size());
- //Length Base Sort
- Collections.sort(FinalData, ( a, b) -> Integer.compare(a.getAbsolutePath().length(), b.getAbsolutePath().length()));
- //print Files
- FinalData.iterator().forEachRemaining(System.out::println);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment