3232 */
3333public class FileUtils {
3434
35+ /**
36+ * Creates a directory if it doesn't exist
37+ * @param dir Directory to be created
38+ * @return Created directory or existing one
39+ */
3540 public static File mkdir (File dir ) {
3641 if (!dir .exists ()) {
3742 dir .mkdirs ();
3843 }
3944 return dir ;
4045 }
4146
47+ /**
48+ * Creates a directory inside an exiting one
49+ * @param parent Parent directory
50+ * @param name New directory name
51+ * @return Created directory
52+ */
4253 public static File mkdir (File parent , String name ) {
4354 File dir = new File (parent , name );
4455 return mkdir (dir );
4556 }
4657
58+ /**
59+ * Copies a file
60+ * @param source Source file
61+ * @param dest Destination file
62+ * @throws Exception If the file cannot be copied
63+ */
4764 public static void copyFileToFile (File source , File dest ) throws Exception {
4865 Logger .info ("Copying file [" + source + "] to file [" + dest + "]" );
4966 try {
@@ -53,6 +70,12 @@ public static void copyFileToFile(File source, File dest) throws Exception {
5370 }
5471 }
5572
73+ /**
74+ * Copies a file inside an existing folder
75+ * @param source File to be copied
76+ * @param destFolder Destination folder
77+ * @throws Exception If the file cannot be copied
78+ */
5679 public static void copyFileToFolder (File source , File destFolder ) throws Exception {
5780 Logger .info ("Copying file [" + source + "] to folder [" + destFolder + "]" );
5881 if (new File (destFolder , source .getName ()).exists ()) return ;
@@ -63,6 +86,12 @@ public static void copyFileToFolder(File source, File destFolder) throws Excepti
6386 }
6487 }
6588
89+ /**
90+ * Concatenates several files into a new one
91+ * @param dest Destination file
92+ * @param sources Source files array
93+ * @throws Exception If a file cannot be writen to the destination
94+ */
6695 public static void concat (File dest , File ... sources ) throws Exception {
6796 Logger .info ("Concatenating files [" + StringUtils .join (sources , "," ) + "] into file [" + dest + "]" );
6897 try {
@@ -188,18 +217,35 @@ public static void removeFolder(File folder) throws Exception {
188217 }
189218 }
190219
220+ /**
221+ * Renames a file
222+ * @param file File to be renamed
223+ * @param newName New file name
224+ */
191225 public static void rename (File file , String newName ) {
192226 Logger .info ("Renaming file [" + file + "] to [" + newName + "]" );
193227 file .renameTo (new File (file .getParentFile (), newName ));
194228 }
195229
230+ /**
231+ * Finds all files in folder that matches the regular expression
232+ * @param searchFolder Searching folder
233+ * @param regex Regular expression
234+ * @return List of found files or an empty list if nothing matches
235+ */
196236 public static List <File > findFiles (File searchFolder , String regex ) {
197237 return Arrays .asList (searchFolder .listFiles ((dir , name ) -> Pattern .matches (regex , name )))
198238 .stream ()
199239 .map (f -> new File (f .getName ()))
200240 .collect (Collectors .toList ());
201241 }
202242
243+ /**
244+ * Finds the first file in folder that matches the regular expression
245+ * @param searchFolder Searching folder
246+ * @param regex Regular expression
247+ * @return Found file or null if nothing matches
248+ */
203249 public static File findFirstFile (File searchFolder , String regex ) {
204250 return Arrays .asList (searchFolder .listFiles ((dir , name ) -> Pattern .matches (regex , name )))
205251 .stream ()
@@ -208,15 +254,32 @@ public static File findFirstFile(File searchFolder, String regex) {
208254 .get ();
209255 }
210256
257+ /**
258+ * Download a resource from an URL to a file
259+ * @param url URL to download
260+ * @param file File to copy the downloaded resource
261+ * @throws IOException Resource cannot be copied/downloaded
262+ */
211263 public static void downloadFromUrl (URL url , File file ) throws IOException {
212264 org .apache .commons .io .FileUtils .copyURLToFile (url , file );
213265 Logger .info ("File downloaded from [" + url + "] to [" + file .getAbsolutePath () + "]" );
214266 }
215267
268+ /**
269+ * Checks if a file exists or is not null
270+ * @param file File
271+ * @return true if file exits, false if doesn't or is null
272+ */
216273 public static boolean exists (File file ) {
217274 return file != null && file .exists ();
218275 }
219276
277+ /**
278+ * Checks if a folder contains a file
279+ * @param folder Searching folder
280+ * @param filename Searched file name
281+ * @return true if folder contains file
282+ */
220283 public static boolean folderContainsFile (File folder , String filename ) {
221284 return new File (folder , filename ).exists ();
222285 }
0 commit comments