|
3 | 3 | import java.io.File; |
4 | 4 | import java.io.FilenameFilter; |
5 | 5 | import java.util.ArrayList; |
| 6 | +import java.util.Collections; |
6 | 7 |
|
7 | 8 | /** |
8 | 9 | * Class for easier file-access and handling filedata |
9 | | - * |
| 10 | + * |
10 | 11 | * @author Paul Seidel |
11 | | - * @version 1.0.4 |
12 | | - * @since 2019-01-01 |
13 | | - * @see Last Updated 2021/08/07 |
| 12 | + * @version 1.0.5 |
| 13 | + * @since 2019-01-01 : Updated 2022-03-14 |
14 | 14 | */ |
15 | 15 | public class PFolder { |
16 | 16 |
|
17 | | - public static File createFolder(String path) { |
18 | | - if (isFile(path)) |
19 | | - return null; |
20 | | - File f = new File(path); |
21 | | - f.mkdirs(); |
22 | | - return f; |
23 | | - } |
24 | | - |
25 | | - public static boolean isFolder(String path) { |
26 | | - return (!isFile(path)); |
27 | | - } |
28 | | - |
29 | | - public static boolean isFolder(File f) { |
30 | | - return ((!isFile(f.getAbsolutePath()))); |
31 | | - } |
32 | | - |
33 | | - public static boolean isFile(String path) { |
34 | | -// System.out.println(" asdfasdf " + ((char) 92)); |
35 | | - for (int i = path.length() - 1; i >= 0; i--) { |
36 | | - if (path.charAt(i) == '/' || path.charAt(i) == ((char) 92)) |
37 | | - return false; |
38 | | - if (path.charAt(i) == '.') |
39 | | - return true; |
40 | | - } |
41 | | - if (new File(path).isFile()) |
42 | | - return true; |
43 | | - return false; |
44 | | - } |
45 | | - |
46 | | - public static String getName(String path) { |
47 | | - String s = ""; |
48 | | - for (int i = path.length() - 1; i >= 0; i--) { |
49 | | - if (path.charAt(i) == '/' || path.charAt(i) == ((char) 92)) |
50 | | - break; |
51 | | - s = path.charAt(i) + s; |
52 | | - } |
53 | | - return s; |
54 | | - } |
55 | | - |
56 | | - public static boolean deleteFolder(String path) { |
57 | | - if (isFile(path)) |
58 | | - return false; |
59 | | - File f = new File(path); |
60 | | - f.delete(); |
61 | | - return true; |
62 | | - } |
63 | | - |
64 | | - /** |
65 | | - * |
66 | | - * @param path of Folder |
67 | | - * @param Type of Files (if is null => Filetypes will be ignored) |
68 | | - * @return |
69 | | - */ |
70 | | - public static String[] getFiles(String path, String fileType) { |
71 | | - String[] s = new File(path).list(); |
72 | | - if (s == null || s.length == 0) |
73 | | - return null; |
74 | | - |
75 | | - ArrayList<String> list = new ArrayList<String>(); |
76 | | - for (int i = 0; i < s.length; i++) |
77 | | - if (isFile(s[i])) |
78 | | - if (fileType == null || s[i].endsWith(fileType)) |
79 | | - list.add(path + "/" + s[i]); |
80 | | - |
81 | | - String sN[] = new String[list.size()]; |
82 | | - for (int i = 0; i < sN.length; i++) |
83 | | - sN[i] = list.get(i); |
84 | | - return sN; |
85 | | - } |
86 | | - |
87 | | - public static String[] getSubFolders(String path) { |
88 | | - String s[] = new File(path).list(); |
89 | | - if (s == null || s.length == 0) |
90 | | - return null; |
91 | | - |
92 | | - ArrayList<String> list = new ArrayList<String>(); |
93 | | - for (int i = 0; i < s.length; i++) |
94 | | - if (!isFile(s[i])) |
95 | | - list.add(path + "/" + s[i]); |
96 | | - |
97 | | - String sN[] = new String[list.size()]; |
98 | | - for (int i = 0; i < sN.length; i++) { |
99 | | - sN[i] = list.get(i); |
100 | | - } |
101 | | - return sN; |
102 | | - } |
103 | | - |
104 | | - /** |
105 | | - * |
106 | | - * @param |
107 | | - * @return All folders and their childfolders recursively inside of it |
108 | | - */ |
109 | | - public static ArrayList<File> getAllFoldersOfRoot(File folder) { |
110 | | - |
111 | | - if (folder != null) { |
112 | | - File[] children = folder.listFiles(new FilenameFilter() { |
113 | | - @Override |
114 | | - public boolean accept(File dir, String name) { |
115 | | - return PFolder.isFolder(name); |
116 | | - } |
117 | | - }); |
118 | | - if (children != null || (children != null && children.length == 0)) { |
119 | | - ArrayList<File> sum = new ArrayList<File>(); |
120 | | - |
121 | | - for (File f : children) { |
122 | | - sum.add(f); |
123 | | - } |
124 | | - |
125 | | - for (int i = 0; i < children.length; i++) { |
126 | | - ArrayList<File> tempFiles = getAllFoldersOfRoot(children[i]); // recursion |
127 | | - if (sum != null && tempFiles != null) { |
128 | | - sum.addAll(tempFiles); |
129 | | - } |
130 | | - } |
131 | | - return sum; |
132 | | - } |
133 | | - } |
134 | | - return null; |
135 | | - } |
| 17 | + public static File createFolder(String path) { |
| 18 | + if (isFile(path)) |
| 19 | + return null; |
| 20 | + File f = new File(path); |
| 21 | + if (f.mkdirs()) |
| 22 | + return f; |
| 23 | + return null; |
| 24 | + } |
| 25 | + |
| 26 | + public static boolean isFolder(String path) { |
| 27 | + return (!isFile(path)); |
| 28 | + } |
| 29 | + |
| 30 | + public static boolean isFolder(File f) { |
| 31 | + return ((!isFile(f.getAbsolutePath()))); |
| 32 | + } |
| 33 | + |
| 34 | + public static boolean isFile(String path) { |
| 35 | + if(path == null) |
| 36 | + return false; |
| 37 | + for (int i = path.length() - 1; i >= 0; i--) { |
| 38 | + if (path.charAt(i) == '/' || path.charAt(i) == ((char) 92)) |
| 39 | + return false; |
| 40 | + if (path.charAt(i) == '.') |
| 41 | + return true; |
| 42 | + } |
| 43 | + return new File(path).isFile(); |
| 44 | + } |
| 45 | + |
| 46 | + public static String getName(String path) { |
| 47 | + String s = ""; |
| 48 | + for (int i = path.length() - 1; i >= 0; i--) { |
| 49 | + if (path.charAt(i) == '/' || path.charAt(i) == ((char) 92)) |
| 50 | + break; |
| 51 | + s = path.charAt(i) + s; |
| 52 | + } |
| 53 | + return s; |
| 54 | + } |
| 55 | + |
| 56 | + public static boolean deleteFolder(String path) { |
| 57 | + if (isFile(path)) |
| 58 | + return false; |
| 59 | + File f = new File(path); |
| 60 | + return f.delete(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param path of Folder |
| 65 | + * @param fileType of Files (if is null => Filetypes will be ignored) |
| 66 | + * @return |
| 67 | + */ |
| 68 | + public static String[] getFiles(String path, String fileType) { |
| 69 | + String[] s = new File(path).list(); |
| 70 | + if (s == null || s.length == 0) |
| 71 | + return null; |
| 72 | + |
| 73 | + ArrayList<String> list = new ArrayList<>(); |
| 74 | + for (String temp : s) |
| 75 | + if (isFile(temp)) |
| 76 | + if (fileType == null || temp.endsWith(fileType)) |
| 77 | + list.add(path + "/" + temp); |
| 78 | + |
| 79 | + String sN[] = new String[list.size()]; |
| 80 | + for (int i = 0; i < sN.length; i++) |
| 81 | + sN[i] = list.get(i); |
| 82 | + return sN; |
| 83 | + } |
| 84 | + |
| 85 | + public static String[] getSubFolders(String path) { |
| 86 | + String s[] = new File(path).list(); |
| 87 | + if (s == null || s.length == 0) |
| 88 | + return null; |
| 89 | + |
| 90 | + ArrayList<String> list = new ArrayList<>(); |
| 91 | + for (String temp : s) |
| 92 | + if (!isFile(temp)) |
| 93 | + list.add(path + "/" + temp); |
| 94 | + |
| 95 | + String sN[] = new String[list.size()]; |
| 96 | + for (int i = 0; i < sN.length; i++) { |
| 97 | + sN[i] = list.get(i); |
| 98 | + } |
| 99 | + return sN; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @param folder Fileobject of folderlocation |
| 104 | + * @return All folders and their childfolders recursively inside of it |
| 105 | + */ |
| 106 | + public static ArrayList<File> getAllFoldersOfRoot(File folder) { |
| 107 | + |
| 108 | + if (folder != null) { |
| 109 | + File[] children = folder.listFiles(new FilenameFilter() { |
| 110 | + @Override |
| 111 | + public boolean accept(File dir, String name) { |
| 112 | + return PFolder.isFolder(name); |
| 113 | + } |
| 114 | + }); |
| 115 | + if (children != null || (children != null && children.length == 0)) { |
| 116 | + ArrayList<File> sum = new ArrayList<>(); |
| 117 | + |
| 118 | + Collections.addAll(sum, children); |
| 119 | + |
| 120 | + for (File temp : children) { |
| 121 | + ArrayList<File> tempFiles = getAllFoldersOfRoot(temp); // recursion |
| 122 | + if (tempFiles != null) { |
| 123 | + sum.addAll(tempFiles); |
| 124 | + } |
| 125 | + } |
| 126 | + return sum; |
| 127 | + } |
| 128 | + } |
| 129 | + return null; |
| 130 | + } |
| 131 | + |
136 | 132 | } |
0 commit comments