Skip to content

Commit 8c598e6

Browse files
author
realPaulsen
committed
v1.1.1d
Small fixes and code-quality-improvements
1 parent 286676a commit 8c598e6

File tree

3 files changed

+143
-145
lines changed

3 files changed

+143
-145
lines changed

src/com/paulsen/io/PFile.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
* Class for easier file-access and handling filedata
1616
*
1717
* @author Paul Seidel
18-
* @version 1.0.4
19-
* @since 2019-01-01
20-
* @see Last Updated 2021/06/24
18+
* @version 1.0.5
19+
* @since 2019-01-01 : Updated 2022-03-14
2120
*/
2221

2322
public class PFile {
2423

2524
/**
26-
* @param Path
25+
* @param path
2726
* @return Filename without ending and folders, where its located
2827
*/
2928
public static String getName(String path) {
@@ -67,8 +66,8 @@ public static boolean deleteFile(String path) {
6766
/**
6867
* copys file if directory is a new one and renames it
6968
*
70-
* @param path of File
71-
* @param new Path to file
69+
* @param file is path of File
70+
* @param target is new Path to File
7271
* @return success
7372
*/
7473
public static boolean copyFile(String file, String target, boolean replaceIfExists) {
@@ -78,14 +77,13 @@ public static boolean copyFile(String file, String target, boolean replaceIfExis
7877
if (!replaceIfExists) {
7978
String addon = " - copy"; // l=8
8079
int copyCount = 0;
81-
while (new File(nPath).exists()) {
80+
while (nPath != null && new File(nPath).exists()) {
8281
if (copyCount > 0) { // remove " - copy0"
83-
String nnP = "";
84-
nnP = remove(nPath, nPath.length() - (addon.length() + PFile.getFileType(nPath).length()) - 2,
82+
String nnP = remove(nPath, nPath.length() - (addon.length() + PFile.getFileType(nPath).length()) - 2,
8583
nPath.length() + PFile.getFileType(nPath).length());
8684
nPath = nnP + "." + PFile.getFileType(nPath);
8785
}
88-
nPath = PFile.getParentFolder(nPath) + "\\" + PFile.getName(nPath) + addon + copyCount + '.'
86+
nPath = PFile.getParentFolder(nPath) + "/" + PFile.getName(nPath) + addon + copyCount + '.'
8987
+ PFile.getFileType(nPath);
9088
copyCount++;
9189
}
@@ -98,7 +96,7 @@ public static boolean copyFile(String file, String target, boolean replaceIfExis
9896
return false;
9997
}
10098

101-
System.out.println("[PFile] :: copyFile :: from " + file + " to " + target);
99+
// System.out.println("[PFile] :: copyFile :: from " + file + " to " + target);
102100

103101
return true;
104102
}
@@ -113,7 +111,7 @@ private static String remove(String s, int a, int b) {
113111
}
114112

115113
/**
116-
* @param Path
114+
* @param file path
117115
* @return filetype (e.g. "txt" "png" "wav" ...)
118116
*/
119117
public static String getFileType(String file) {
@@ -135,11 +133,11 @@ public PFile(String path) {
135133
File file = new File(path);
136134

137135
if (file.exists()) {
138-
System.out.println("[PFile] :: " + getName(path) + "." + getFileType(path) + " already exists");
136+
// System.out.println("[PFile] :: " + getName(path) + "." + getFileType(path) + " already exists");
139137
} else {
140138
try {
141139
file.createNewFile();
142-
System.out.println("[PFile] :: " + getName(path) + "." + getFileType(path) + " created");
140+
// System.out.println("[PFile] :: " + getName(path) + "." + getFileType(path) + " created");
143141
} catch (IOException e) {
144142
}
145143
}
@@ -168,7 +166,6 @@ public static String[] getParagraphs(String s) {
168166
}
169167
if (lastword.length() != 0) {
170168
out.add(lastword);
171-
lastword = "";
172169
}
173170
String array[] = new String[out.size()];
174171
for (int i = 0; i < out.size(); i++)
@@ -196,7 +193,6 @@ public synchronized String[] getAllParagraphs() {
196193
}
197194
if (lastword.length() != 0) {
198195
out.add(lastword);
199-
lastword = "";
200196
}
201197
}
202198

@@ -230,7 +226,7 @@ public synchronized String getFileAsString() {
230226
* @return Array of Strings that were seperated with LINES and not with space
231227
*/
232228
public synchronized String[] getLines() {
233-
ArrayList<String> lines = new ArrayList<String>();
229+
ArrayList<String> lines = new ArrayList<>();
234230

235231
if (exists()) {
236232
Scanner scn;
@@ -252,14 +248,14 @@ public synchronized String[] getLines() {
252248
/**
253249
* Overwrites file with inhalt
254250
*
255-
* @param inhalt
251+
* @param content contains whole fileContent (including linebreaks)
256252
*/
257-
public synchronized void writeFile(String inhalt) {
253+
public synchronized void writeFile(String content) {
258254
try {
259255
FileWriter fileWriter;
260256
fileWriter = new FileWriter(path);
261257
PrintWriter printWriter = new PrintWriter(fileWriter);
262-
printWriter.print(inhalt);
258+
printWriter.print(content);
263259
printWriter.close();
264260
} catch (IOException e) {
265261
}

src/com/paulsen/io/PFolder.java

Lines changed: 119 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -3,134 +3,130 @@
33
import java.io.File;
44
import java.io.FilenameFilter;
55
import java.util.ArrayList;
6+
import java.util.Collections;
67

78
/**
89
* Class for easier file-access and handling filedata
9-
*
10+
*
1011
* @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
1414
*/
1515
public class PFolder {
1616

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+
136132
}

0 commit comments

Comments
 (0)