View difference between Paste ID: hzgZXDX0 and swHMPrZf
SHOW: | | - or go back to the newest paste.
1-
import java.io.File;
1+
import java.io.File;
2-
import java.util.ArrayList;
2+
import java.util.ArrayList;
3-
import java.util.Arrays;
3+
import java.util.Arrays;
4-
import java.util.Collections;
4+
import java.util.Collections;
5-
import java.util.List;
5+
import java.util.List;
6-
6+
7-
/**
7+
/**
8-
 *
8+
 *
9-
 * @author Aamir khan
9+
 * @author Aamir khan
10-
 */
10+
 */
11-
public class EbookShop {
11+
public class EbookShop {
12-
12+
13-
    private static  List<File> FinalData;
13+
    private static  List<File> FinalData;
14-
    public List<File> allFilesAndFolders, SubFolders;
14+
    public List<File> allFilesAndFolders, SubFolders;
15-
    private final File ROOT = new File("F:\\extra\\ebooks\\Blender");
15+
    private final File ROOT = new File("PATH TO THE ROOT FOLDER");
16-
16+
17-
17+
18-
    public EbookShop() {
18+
    public EbookShop() {
19-
        EbookShop.FinalData = new ArrayList();
19+
        EbookShop.FinalData = new ArrayList();
20-
        this.allFilesAndFolders = new ArrayList();
20+
        this.allFilesAndFolders = new ArrayList();
21-
        this.SubFolders = new ArrayList();
21+
        this.SubFolders = new ArrayList();
22-
    }
22+
    }
23-
23+
24-
    //Fire Up
24+
    //Fire Up
25-
    public void Fire() {
25+
    public void Fire() {
26-
26+
27-
        //get All Files and Folders
27+
        //get All Files and Folders
28-
        allFilesAndFolders.addAll(Arrays.asList(ROOT.listFiles()));
28+
        allFilesAndFolders.addAll(Arrays.asList(ROOT.listFiles()));
29-
        //Extract Folders
29+
        //Extract Folders
30-
        allFilesAndFolders.iterator().forEachRemaining(x -> {
30+
        allFilesAndFolders.iterator().forEachRemaining(x -> {
31-
            try {
31+
            try {
32-
                if (x.isDirectory()) {
32+
                if (x.isDirectory()) {
33-
                    SearchFiles(x);
33+
                    SearchFiles(x);
34-
                    if (hasSubFolders(x)) {
34+
                    if (hasSubFolders(x)) {
35-
                        getSubFolders(x);
35+
                        getSubFolders(x);
36-
                    }
36+
                    }
37-
                    
37+
                    
38-
                }else{
38+
                }else{
39-
                //Take the Files out
39+
                //Take the Files out
40-
                CheckAndAdd(x);
40+
                CheckAndAdd(x);
41-
                }
41+
                }
42-
                
42+
                
43-
            } catch (Exception e) {
43+
            } catch (Exception e) {
44-
                e.printStackTrace();
44+
                e.printStackTrace();
45-
            }
45+
            }
46-
        });
46+
        });
47-
        //Get Files From SubFolders
47+
        //Get Files From SubFolders
48-
        SubFolders.iterator().forEachRemaining(y -> SearchFiles(y));
48+
        SubFolders.iterator().forEachRemaining(y -> SearchFiles(y));
49-
        
49+
        
50-
    }
50+
    }
51-
51+
52-
52+
53-
    public final File[] listFolders(File path) {
53+
    public final File[] listFolders(File path) {
54-
        File[] listFiles = null;
54+
        File[] listFiles = null;
55-
        try {
55+
        try {
56-
            //list the Folders only those have Folders/Files inside
56+
            //list the Folders only those have Folders/Files inside
57-
            listFiles = path.listFiles((File pathname)
57+
            listFiles = path.listFiles((File pathname)
58-
                    -> {
58+
                    -> {
59-
                        return pathname.isDirectory()//make sure its Directory
59+
                        return pathname.isDirectory()//make sure its Directory
60-
                        && !(pathname.isHidden())//Do not include any hidden File/Folder
60+
                        && !(pathname.isHidden())//Do not include any hidden File/Folder
61-
                        //Make Sure its not Empty
61+
                        //Make Sure its not Empty
62-
                        && pathname.getAbsoluteFile().listFiles(
62+
                        && pathname.getAbsoluteFile().listFiles(
63-
                                (File pathname1) -> pathname1.isDirectory() || pathname1.isFile()).length != 0;
63+
                                (File pathname1) -> pathname1.isDirectory() || pathname1.isFile()).length != 0;
64-
                    }
64+
                    }
65-
            );
65+
            );
66-
66+
67-
        } catch (Exception e) {
67+
        } catch (Exception e) {
68-
            e.printStackTrace();
68+
            e.printStackTrace();
69-
        }
69+
        }
70-
        return listFiles;
70+
        return listFiles;
71-
    }
71+
    }
72-
72+
73-
    public final void SearchFiles(File folder) {
73+
    public final void SearchFiles(File folder) {
74-
        if (folder.isDirectory()) {
74+
        if (folder.isDirectory()) {
75-
            File[] listFiles = folder.listFiles(path -> CheckForExtension(path));
75+
            File[] listFiles = folder.listFiles(path -> CheckForExtension(path));
76-
            for (File temp : listFiles) {
76+
            for (File temp : listFiles) {
77-
                CheckAndAdd(temp);
77+
                CheckAndAdd(temp);
78-
            }
78+
            }
79-
        }
79+
        }
80-
80+
81-
    }
81+
    }
82-
82+
83-
    private boolean CheckAndAdd(File folder) {
83+
    private boolean CheckAndAdd(File folder) {
84-
        boolean flag = false;
84+
        boolean flag = false;
85-
        if (CheckForExtension(folder)) {
85+
        if (CheckForExtension(folder)) {
86-
            flag = FinalData.add(folder);
86+
            flag = FinalData.add(folder);
87-
87+
88-
        }
88+
        }
89-
89+
90-
        return flag;
90+
        return flag;
91-
    }
91+
    }
92-
92+
93-
    private boolean CheckForExtension(File file) {
93+
    private boolean CheckForExtension(File file) {
94-
        return (file.getName().toLowerCase().endsWith(".pdf"));
94+
        return (file.getName().toLowerCase().endsWith(".pdf"));
95-
//                || file.getName().toLowerCase().endsWith(".7z")
95+
//                || file.getName().toLowerCase().endsWith(".7z")
96-
//                || file.getName().toLowerCase().endsWith(".zip")
96+
//                || file.getName().toLowerCase().endsWith(".zip")
97-
//                || file.getName().toLowerCase().endsWith(".rar"));
97+
//                || file.getName().toLowerCase().endsWith(".rar"));
98-
    }
98+
    }
99-
99+
100-
    private boolean hasSubFolders(File x) {
100+
    private boolean hasSubFolders(File x) {
101-
        File[] listFolders = listFolders(x);
101+
        File[] listFolders = listFolders(x);
102-
        return (listFolders.length != 0);
102+
        return (listFolders.length != 0);
103-
    }
103+
    }
104-
104+
105-
    private void getSubFolders(File x) {
105+
    private void getSubFolders(File x) {
106-
        File[] listFolders = listFolders(x);
106+
        File[] listFolders = listFolders(x);
107-
        SubFolders.addAll(Arrays.asList(listFolders));
107+
        SubFolders.addAll(Arrays.asList(listFolders));
108-
    }
108+
    }
109-
    public static void main(String[] args)throws java.io.IOException {
109+
    public static void main(String[] args)throws java.io.IOException {
110-
        new EbookShop().Fire();
110+
        new EbookShop().Fire();
111-
        //get the Final Result
111+
        //get the Final Result
112-
        System.out.println("Total Files Found "+FinalData.size());
112+
        System.out.println("Total Files Found "+FinalData.size());
113-
        //Length Base Sort
113+
        //Length Base Sort
114-
        Collections.sort(FinalData, ( a,  b) -> Integer.compare(a.getAbsolutePath().length(), b.getAbsolutePath().length()));
114+
        Collections.sort(FinalData, ( a,  b) -> Integer.compare(a.getAbsolutePath().length(), b.getAbsolutePath().length()));
115-
        //print Files
115+
        //print Files
116-
        FinalData.iterator().forEachRemaining(System.out::println);
116+
        FinalData.iterator().forEachRemaining(System.out::println);
117-
    }
117+
    }
118-
118+
119
}