@@ -37,6 +37,7 @@ public class FileUtils {
3737
3838 /**
3939 * Creates a directory if it doesn't exist
40+ *
4041 * @param dir Directory to be created
4142 * @return Created directory or existing one
4243 */
@@ -46,65 +47,70 @@ public static File mkdir(File dir) {
4647 }
4748 return dir ;
4849 }
49-
50+
5051 /**
5152 * Creates a directory inside an exiting one
53+ *
5254 * @param parent Parent directory
53- * @param name New directory name
55+ * @param name New directory name
5456 * @return Created directory
5557 */
5658 public static File mkdir (File parent , String name ) {
5759 File dir = new File (parent , name );
5860 return mkdir (dir );
5961 }
60-
62+
6163 /**
6264 * Copies a file
65+ *
6366 * @param source Source file
64- * @param dest Destination file
67+ * @param dest Destination file
6568 * @throws Exception If the file cannot be copied
6669 */
6770 public static void copyFileToFile (File source , File dest ) throws Exception {
68- Logger .info ("Copying file [" + source + "] to file [" + dest + "]" );
71+ Logger .info ("Copying file [" + source + "] to file [" + dest + "]" );
6972 try {
7073 copyFile (source , dest );
7174 } catch (IOException e ) {
7275 throw new Exception (e .getMessage (), e );
7376 }
7477 }
75-
78+
7679 /**
77- * Copies a file inside an existing folder
78- * @param source File to be copied
80+ * Copies a file inside an existing folder
81+ *
82+ * @param source File to be copied
7983 * @param destFolder Destination folder
8084 * @throws Exception If the file cannot be copied
8185 */
8286 public static void copyFileToFolder (File source , File destFolder ) throws Exception {
8387 copyFileToFolder (source , destFolder , false );
8488 }
85-
89+
8690 public static void copyFileToFolder (File source , File destFolder , boolean overwrite ) throws Exception {
8791 Logger .info ("Copying file [" + source + "] to folder [" + destFolder + "]" );
8892 File destFile = new File (destFolder , source .getName ());
89- if (destFile .exists () && !overwrite ) return ;
93+ if (destFile .exists () && !overwrite )
94+ return ;
9095 try {
9196 if (Platform .windows .isCurrentPlatform ())
9297 Files .copy (source .toPath (), destFile .toPath (), StandardCopyOption .COPY_ATTRIBUTES );
9398 else {
9499 CommandUtils .execute ("cp" , source , destFile );
95- }
100+ }
96101 } catch (IOException e ) {
97102 throw new Exception (e .getMessage (), e );
98103 }
99104 }
100-
105+
101106 /**
102107 * Concatenates several files into a new one
103- * @param dest Destination file
108+ *
109+ * @param dest Destination file
104110 * @param sources Source files array
105- * @throws Exception If a file cannot be writen to the destination
111+ * @throws Exception If a file cannot be writen to the destination
106112 */
107- public static void concat (File dest , File ... sources ) throws Exception {
113+ public static void concat (File dest , File ... sources ) throws Exception {
108114 Logger .info ("Concatenating files [" + StringUtils .join (sources , "," ) + "] into file [" + dest + "]" );
109115 try {
110116 FileOutputStream fos = new FileOutputStream (dest );
@@ -119,10 +125,11 @@ public static void concat(File dest, File ... sources) throws Exception {
119125 throw new Exception ("Error concatenating streams" , e );
120126 }
121127 }
122-
128+
123129 public static void copyFolderToFolder (File from , File to ) throws Exception {
124130 Logger .info ("Copying folder [" + from + "] to folder [" + to + "]" );
125- if (!from .isDirectory ()) throw new Exception ("Source folder " + from + " is not a directory" );
131+ if (!from .isDirectory ())
132+ throw new Exception ("Source folder " + from + " is not a directory" );
126133 try {
127134 if (Platform .windows .isCurrentPlatform ())
128135 copyDirectoryToDirectory (from , to );
@@ -133,12 +140,15 @@ public static void copyFolderToFolder(File from, File to) throws Exception {
133140 throw new Exception (e .getMessage (), e );
134141 }
135142 }
136-
143+
137144 public static void copyFolderContentToFolder (File from , File to ) throws Exception {
138145 Logger .info ("Copying folder content [" + from + "] to folder [" + to + "]" );
139- if (!from .isDirectory ()) throw new Exception ("Source folder " + from + " is not a directory" );
140- if (!to .exists ()) to .mkdirs ();
141- else if (!to .isDirectory ()) throw new Exception ("Destination folder " + to + " is not a directory" );
146+ if (!from .isDirectory ())
147+ throw new Exception ("Source folder " + from + " is not a directory" );
148+ if (!to .exists ())
149+ to .mkdirs ();
150+ else if (!to .isDirectory ())
151+ throw new Exception ("Destination folder " + to + " is not a directory" );
142152 for (File file : from .listFiles ()) {
143153 if (file .isDirectory ())
144154 copyFolderToFolder (file , to );
@@ -149,19 +159,23 @@ public static void copyFolderContentToFolder(File from, File to) throws Exceptio
149159
150160 public static void moveFolderToFolder (File from , File to ) throws Exception {
151161 Logger .info ("Moving folder [" + from + "] to folder [" + to + "]" );
152- if (!from .isDirectory ()) throw new Exception ("Source folder " + from + " is not a directory" );
153- else if (to .exists () && !to .isDirectory ()) throw new Exception ("Destination folder " + to + " is not a directory" );
162+ if (!from .isDirectory ())
163+ throw new Exception ("Source folder " + from + " is not a directory" );
164+ else if (to .exists () && !to .isDirectory ())
165+ throw new Exception ("Destination folder " + to + " is not a directory" );
154166 try {
155167 moveDirectoryToDirectory (from , to , true );
156168 } catch (IOException e ) {
157169 throw new Exception (e .getMessage (), e );
158170 }
159171 }
160-
172+
161173 public static void moveFolderContentToFolder (File from , File to ) throws Exception {
162174 Logger .info ("Moving folder content [" + from + "] to folder [" + to + "]" );
163- if (!from .isDirectory ()) throw new Exception ("Source folder " + from + " is not a directory" );
164- else if (!to .isDirectory ()) throw new Exception ("Destination folder " + to + " is not a directory" );
175+ if (!from .isDirectory ())
176+ throw new Exception ("Source folder " + from + " is not a directory" );
177+ else if (!to .isDirectory ())
178+ throw new Exception ("Destination folder " + to + " is not a directory" );
165179 try {
166180 for (File file : from .listFiles ()) {
167181 if (file .isDirectory ())
@@ -175,29 +189,32 @@ public static void moveFolderContentToFolder(File from, File to) throws Exceptio
175189 }
176190
177191 public static void moveFileToFolder (File from , File to ) throws Exception {
178- Logger .info ("Moving file [" + from + "] to folder [" + to + "]" );
179- if (!from .isFile ()) throw new Exception ("Source file " + from + " is not a file" );
180- if (!to .exists ()) to .mkdirs ();
192+ Logger .info ("Moving file [" + from + "] to folder [" + to + "]" );
193+ if (!from .isFile ())
194+ throw new Exception ("Source file " + from + " is not a file" );
195+ if (!to .exists ())
196+ to .mkdirs ();
181197 try {
182198 moveFileToDirectory (from , to , true );
183199 } catch (IOException e ) {
184200 throw new Exception (e .getMessage (), e );
185201 }
186202 }
187-
203+
188204 private static void copyStreamToFile (InputStream is , File dest ) throws Exception {
189- try {
190- copyInputStreamToFile (is , dest );
191- } catch (IOException ex ) {
192- throw new Exception ("Could not copy input stream to " + dest , ex );
193- }
205+ try {
206+ copyInputStreamToFile (is , dest );
207+ } catch (IOException ex ) {
208+ throw new Exception ("Could not copy input stream to " + dest , ex );
209+ }
194210 }
195211
196212 public static void copyResourceToFile (String resource , File dest , boolean unixStyleNewLines ) throws Exception {
197213 copyResourceToFile (resource , dest , unixStyleNewLines , null );
198214 }
199215
200- public static void copyResourceToFile (String resource , File dest , boolean unixStyleNewLines , File assetsDir ) throws Exception {
216+ public static void copyResourceToFile (String resource , File dest , boolean unixStyleNewLines , File assetsDir )
217+ throws Exception {
201218 copyResourceToFile (resource , dest , assetsDir );
202219 if (unixStyleNewLines ) {
203220 try {
@@ -207,7 +224,7 @@ public static void copyResourceToFile(String resource, File dest, boolean unixSt
207224 }
208225 }
209226 }
210-
227+
211228 public static void processFileContent (File dest , Function <String , String > function ) throws IOException {
212229 String content = readFileToString (dest , StandardCharsets .UTF_8 );
213230 content = function .apply (content );
@@ -218,7 +235,7 @@ public static void copyResourceToFile(String resource, File dest) throws Excepti
218235 copyResourceToFile (resource , dest , null );
219236 }
220237
221- public static void copyResourceToFile (String resource , File dest , File assetsDir ) throws Exception {
238+ public static void copyResourceToFile (String resource , File dest , File assetsDir ) throws Exception {
222239 if (assetsDir != null ) {
223240 String rsc = resource .startsWith ("/" ) ? resource .substring (1 ) : resource ;
224241 Path asset = assetsDir .toPath ().resolve (rsc );
@@ -228,109 +245,116 @@ public static void copyResourceToFile(String resource, File dest, File assetsDir
228245 return ;
229246 }
230247 }
231- Logger .info ("Copying resource [" + resource + "] to file [" + dest + "]" );
248+ Logger .info ("Copying resource [" + resource + "] to file [" + dest + "]" );
232249 copyStreamToFile (FileUtils .class .getResourceAsStream (resource ), dest );
233250 }
234-
251+
235252 public static void createSymlink (File link , File target ) throws Exception {
236- Logger .info ("Creating symbolic link [" + link + "] to [" + target + "]" );
237- try {
253+ Logger .info ("Creating symbolic link [" + link + "] to [" + target + "]" );
254+ try {
238255 Files .createSymbolicLink (link .toPath (), target .toPath ());
239256 } catch (IOException e ) {
240257 throw new Exception ("Could not create symlink " + link + " to " + target , e );
241258 }
242259 }
243-
260+
244261 public static void removeFolder (File folder ) throws Exception {
245- Logger .info ("Removing folder [" + folder + "]" );
262+ Logger .info ("Removing folder [" + folder + "]" );
246263 try {
247- deleteDirectory (folder );
264+ if (Platform .windows .isCurrentPlatform ())
265+ deleteDirectory (folder );
266+ else {
267+ CommandUtils .execute ("rm" , "-fr" , folder );
268+ }
248269 } catch (IOException e ) {
249- throw new Exception ("Could not remove folder " + folder , e );
270+ throw new Exception ("Could not remove folder " + folder , e );
250271 }
251272 }
252-
273+
253274 /**
254275 * Renames a file
255- * @param file File to be renamed
276+ *
277+ * @param file File to be renamed
256278 * @param newName New file name
257279 */
258280 public static void rename (File file , String newName ) {
259- Logger .info ("Renaming file [" + file + "] to [" + newName + "]" );
260- file .renameTo (new File (file .getParentFile (), newName ));
281+ Logger .info ("Renaming file [" + file + "] to [" + newName + "]" );
282+ file .renameTo (new File (file .getParentFile (), newName ));
261283 }
262-
284+
263285 /**
264286 * Finds all files in folder that matches the regular expression
287+ *
265288 * @param searchFolder Searching folder
266- * @param regex Regular expression
267- * @return List of found files or an empty list if nothing matches
289+ * @param regex Regular expression
290+ * @return List of found files or an empty list if nothing matches
268291 */
269292 public static List <File > findFiles (File searchFolder , String regex ) {
270- return Arrays .asList (searchFolder .listFiles ((dir , name ) -> Pattern .matches (regex , name )))
271- .stream ()
272- .map (f -> new File (f .getName ()))
273- .collect (Collectors .toList ());
293+ return Arrays .asList (searchFolder .listFiles ((dir , name ) -> Pattern .matches (regex , name ))).stream ()
294+ .map (f -> new File (f .getName ())).collect (Collectors .toList ());
274295 }
275-
296+
276297 /**
277298 * Finds the first file in folder that matches the regular expression
299+ *
278300 * @param searchFolder Searching folder
279- * @param regex Regular expression
280- * @return Found file or null if nothing matches
301+ * @param regex Regular expression
302+ * @return Found file or null if nothing matches
281303 */
282304 public static File findFirstFile (File searchFolder , String regex ) {
283- return Arrays .asList (searchFolder .listFiles ((dir , name ) -> Pattern .matches (regex , name )))
284- .stream ()
285- .map (f -> new File (f .getName ()))
286- .findFirst ()
287- .get ();
305+ return Arrays .asList (searchFolder .listFiles ((dir , name ) -> Pattern .matches (regex , name ))).stream ()
306+ .map (f -> new File (f .getName ())).findFirst ().get ();
288307 }
289-
308+
290309 /**
291310 * Download a resource from an URL to a file
292- * @param url URL to download
311+ *
312+ * @param url URL to download
293313 * @param file File to copy the downloaded resource
294314 * @throws IOException Resource cannot be copied/downloaded
295315 */
296316 public static void downloadFromUrl (URL url , File file ) throws IOException {
297317 org .apache .commons .io .FileUtils .copyURLToFile (url , file );
298318 Logger .info ("File downloaded from [" + url + "] to [" + file .getAbsolutePath () + "]" );
299319 }
300-
320+
301321 /**
302322 * Download a resource from an URL to a file
303- * @param url URL to download
323+ *
324+ * @param url URL to download
304325 * @param file File to copy the downloaded resource
305326 * @throws IOException Resource cannot be copied/downloaded
306327 */
307328 public static void downloadFromUrl (String url , File file ) throws IOException {
308329 downloadFromUrl (new URL (url ), file );
309330 }
310-
331+
311332 /**
312333 * Checks if a file exists or is not null
334+ *
313335 * @param file File
314336 * @return true if file exits, false if doesn't or is null
315337 */
316338 public static boolean exists (File file ) {
317339 return file != null && file .exists ();
318340 }
319-
341+
320342 /**
321343 * Checks if a folder contains a file
322- * @param folder Searching folder
344+ *
345+ * @param folder Searching folder
323346 * @param filename Searched file name
324347 * @return true if folder contains file
325348 */
326349 public static boolean folderContainsFile (File folder , String filename ) {
327350 return new File (folder , filename ).exists ();
328351 }
329-
352+
330353 /**
331- * Writes String to a file in UTF-8 including BOM
354+ * Writes String to a file in UTF-8 including BOM
355+ *
332356 * @param output Output file
333- * @param data Input string
357+ * @param data Input string
334358 * @throws Exception if something goes wrong
335359 */
336360 public static void writeStringToFileWithBOM (File output , String data ) throws Exception {
0 commit comments