5252 * individuals on behalf of the Apache Software Foundation. For more
5353 * information on the Apache Software Foundation, please see
5454 * <http://www.apache.org/>.
55+ *
5556 */
5657
58+
5759import java .io .File ;
5860import java .io .FileInputStream ;
5961import java .io .FileOutputStream ;
6062import java .util .Vector ;
6163
64+
6265/**
6366 * Common {@link java.io.File} manipulation routines.
6467 *
6972 * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
7073 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
7174 * @author <a href="mailto:Christoph.Reck@dlr.de">Christoph.Reck</a>
72- * @version $Id: FileUtils.java,v 1.2 2002/01/28 04:44:49 sanders Exp $
75+ * @version $Id: FileUtils.java,v 1.3 2002/01/28 05:01:48 sanders Exp $
7376 */
74- public class FileUtils
75- {
77+ public class FileUtils {
78+
7679 /**
7780 * The number of bytes in a kilobyte.
7881 */
@@ -95,24 +98,16 @@ public class FileUtils
9598 * @param size The number of bytes.
9699 * @return A human-readable display value (includes units).
97100 */
98- public static String byteCountToDisplaySize (int size )
99- {
101+ public static String byteCountToDisplaySize (int size ) {
100102 String displaySize ;
101103
102- if (size / ONE_GB > 0 )
103- {
104+ if (size / ONE_GB > 0 ) {
104105 displaySize = String .valueOf (size / ONE_GB ) + " GB" ;
105- }
106- else if (size / ONE_MB > 0 )
107- {
106+ } else if (size / ONE_MB > 0 ) {
108107 displaySize = String .valueOf (size / ONE_MB ) + " MB" ;
109- }
110- else if (size / ONE_KB > 0 )
111- {
108+ } else if (size / ONE_KB > 0 ) {
112109 displaySize = String .valueOf (size / ONE_KB ) + " KB" ;
113- }
114- else
115- {
110+ } else {
116111 displaySize = String .valueOf (size ) + " bytes" ;
117112 }
118113
@@ -124,8 +119,7 @@ else if (size / ONE_KB > 0)
124119 * Matches the equally named unix command.
125120 * @return The directory portion excluding the ending file separator.
126121 */
127- public static String dirname (String filename )
128- {
122+ public static String dirname (String filename ) {
129123 int i = filename .lastIndexOf (File .separator );
130124 return (i >= 0 ? filename .substring (0 , i ) : "" );
131125 }
@@ -134,8 +128,7 @@ public static String dirname(String filename)
134128 * Returns the filename portion of a file specification string.
135129 * @return The filename string with extension.
136130 */
137- public static String filename (String filename )
138- {
131+ public static String filename (String filename ) {
139132 int i = filename .lastIndexOf (File .separator );
140133 return (i >= 0 ? filename .substring (i + 1 ) : filename );
141134 }
@@ -145,32 +138,25 @@ public static String filename(String filename)
145138 * Matches the equally named unix command.
146139 * @return The filename string without extension.
147140 */
148- public static String basename (String filename )
149- {
141+ public static String basename (String filename ) {
150142 return basename (filename , extension (filename ));
151143 }
152144
153145 /**
154146 * Returns the filename portion of a file specification string.
155147 * Matches the equally named unix command.
156148 */
157- public static String basename (String filename , String suffix )
158- {
149+ public static String basename (String filename , String suffix ) {
159150 int i = filename .lastIndexOf (File .separator ) + 1 ;
160151 int lastDot = ((suffix != null ) && (suffix .length () > 0 ))
161- ? filename .lastIndexOf (suffix ) : -1 ;
162-
163- if (lastDot >= 0 )
164- {
165- return filename .substring (i , lastDot );
166- }
167- else if (i > 0 )
168- {
169- return filename .substring (i );
170- }
171- else
172- {
173- return filename ; // else returns all (no path and no extension)
152+ ? filename .lastIndexOf (suffix ) : -1 ;
153+
154+ if (lastDot >= 0 ) {
155+ return filename .substring (i , lastDot );
156+ } else if (i > 0 ) {
157+ return filename .substring (i );
158+ } else {
159+ return filename ; // else returns all (no path and no extension)
174160 }
175161 }
176162
@@ -179,17 +165,13 @@ else if (i > 0)
179165 * This everything after the last dot '.' in the filename (NOT including
180166 * the dot).
181167 */
182- public static String extension (String filename )
183- {
168+ public static String extension (String filename ) {
184169 int lastDot = filename .lastIndexOf ('.' );
185170
186- if (lastDot >= 0 )
187- {
188- return filename .substring (lastDot + 1 );
189- }
190- else
191- {
192- return "" ;
171+ if (lastDot >= 0 ) {
172+ return filename .substring (lastDot + 1 );
173+ } else {
174+ return "" ;
193175 }
194176 }
195177
@@ -199,8 +181,7 @@ public static String extension(String filename)
199181 * @param fileName The name of the file to check.
200182 * @return true if file exists.
201183 */
202- public static boolean fileExists (String fileName )
203- {
184+ public static boolean fileExists (String fileName ) {
204185 File file = new File (fileName );
205186 return file .exists ();
206187 }
@@ -211,17 +192,16 @@ public static boolean fileExists(String fileName)
211192 * @param fileName The name of the file to read.
212193 * @return The file contents or null if read failed.
213194 */
214- public static String fileRead (String fileName ) throws Exception
215- {
195+ public static String fileRead (String fileName ) throws Exception {
216196 StringBuffer buf = new StringBuffer ();
217197
218198 FileInputStream in = new FileInputStream (fileName );
219199
220200 int count ;
221201 byte [] b = new byte [512 ];
222- while ( (count = in .read (b )) > 0 ) // blocking read
202+ while ((count = in .read (b )) > 0 ) // blocking read
223203 {
224- buf .append ( new String (b , 0 , count ) );
204+ buf .append (new String (b , 0 , count ));
225205 }
226206
227207 in .close ();
@@ -235,10 +215,9 @@ public static String fileRead(String fileName) throws Exception
235215 * @param fileName The name of the file to write.
236216 * @param data The content to write to the file.
237217 */
238- public static void fileWrite (String fileName , String data ) throws Exception
239- {
218+ public static void fileWrite (String fileName , String data ) throws Exception {
240219 FileOutputStream out = new FileOutputStream (fileName );
241- out .write ( data .getBytes () );
220+ out .write (data .getBytes ());
242221 out .close ();
243222 }
244223
@@ -247,8 +226,7 @@ public static void fileWrite(String fileName, String data) throws Exception
247226 *
248227 * @param fileName The name of the file to delete.
249228 */
250- public static void fileDelete (String fileName )
251- {
229+ public static void fileDelete (String fileName ) {
252230 File file = new File (fileName );
253231 file .delete ();
254232 }
@@ -260,30 +238,21 @@ public static void fileDelete(String fileName)
260238 * @param seconds The maximum time in seconds to wait.
261239 * @return True if file exists.
262240 */
263- public static boolean waitFor (String fileName , int seconds )
264- {
241+ public static boolean waitFor (String fileName , int seconds ) {
265242 File file = new File (fileName );
266243 int timeout = 0 ;
267244 int tick = 0 ;
268- while ( ! file .exists () )
269- {
270- if (tick ++ >= 10 )
271- {
245+ while (!file .exists ()) {
246+ if (tick ++ >= 10 ) {
272247 tick = 0 ;
273- if (timeout ++ > seconds )
274- {
248+ if (timeout ++ > seconds ) {
275249 return false ;
276250 }
277251 }
278- try
279- {
252+ try {
280253 Thread .sleep (100 );
281- }
282- catch (InterruptedException ignore )
283- {
284- }
285- catch (Exception ex )
286- {
254+ } catch (InterruptedException ignore ) {
255+ } catch (Exception ex ) {
287256 break ;
288257 }
289258 }
@@ -296,8 +265,7 @@ public static boolean waitFor(String fileName, int seconds)
296265 * @param fileName The name of the file.
297266 * @return A <code>File</code> instance.
298267 */
299- public static File getFile (String fileName )
300- {
268+ public static File getFile (String fileName ) {
301269 return new File (fileName );
302270 }
303271
@@ -309,7 +277,7 @@ public static File getFile(String fileName)
309277 * TODO Should a recurse flag be passed in?
310278 *
311279 * The given extensions should be like "java" and not like ".java"
312- */
280+ */
313281 public static String [] getFilesFromExtension (String directory , String [] extensions ) {
314282
315283 Vector files = new Vector ();
@@ -322,15 +290,15 @@ public static String[] getFilesFromExtension(String directory, String[] extensio
322290 return new String [0 ];
323291 }
324292
325- for (int i = 0 ;i < unknownFiles .length ;++i ) {
293+ for (int i = 0 ; i < unknownFiles .length ; ++i ) {
326294 String currentFileName = directory + System .getProperty ("file.separator" ) + unknownFiles [i ];
327295 java .io .File currentFile = new java .io .File (currentFileName );
328296
329297 if (currentFile .isDirectory ()) {
330298
331299
332300 //ignore all CVS directories...
333- if ( currentFile .getName ().equals ("CVS" ) ) {
301+ if (currentFile .getName ().equals ("CVS" )) {
334302 continue ;
335303 }
336304
@@ -339,14 +307,14 @@ public static String[] getFilesFromExtension(String directory, String[] extensio
339307 //them with the current list.
340308
341309 String [] fetchFiles = getFilesFromExtension (currentFileName , extensions );
342- files = blendFilesToVector ( files , fetchFiles );
310+ files = blendFilesToVector (files , fetchFiles );
343311
344312 } else {
345313 //ok... add the file
346314
347315 String add = currentFile .getAbsolutePath ();
348- if ( isValidFile ( add , extensions ) ) {
349- files .addElement ( add );
316+ if (isValidFile (add , extensions ) ) {
317+ files .addElement (add );
350318
351319 }
352320
@@ -365,7 +333,7 @@ public static String[] getFilesFromExtension(String directory, String[] extensio
365333
366334 /**
367335 * Private hepler method for getFilesFromExtension()
368- */
336+ */
369337 private static Vector blendFilesToVector (Vector v , String [] files ) {
370338
371339 for (int i = 0 ; i < files .length ; ++i ) {
@@ -380,7 +348,7 @@ private static Vector blendFilesToVector(Vector v, String[] files) {
380348 * Note that if the file does not have an extension, an empty string
381349 * ("") is matched for.
382350 *
383- */
351+ */
384352 private static boolean isValidFile (String file , String [] extensions ) {
385353
386354
0 commit comments