6363 * @author Martin Cooper
6464 * @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
6565 * @author Stephen Colebourne
66- * @version $Id: FilenameUtils.java,v 1.21 2004/10/30 22:43:21 scolebourne Exp $
66+ * @version $Id: FilenameUtils.java,v 1.22 2004/10/30 23:12:18 scolebourne Exp $
6767 * @since Commons IO 1.1
6868 */
6969public class FilenameUtils {
@@ -128,121 +128,6 @@ public static String removeExtension(String filename) {
128128 return filename .substring (0 , index );
129129 }
130130
131- /**
132- * Gets the extension of a filename.
133- * <p>
134- * eg
135- * <pre>
136- * foo.txt --> "txt"
137- * a/b/c.jpg --> "jpg"
138- * a/b/c --> ""
139- * a.b/c.txt --> "txt"
140- * a.b/c --> ""
141- * </pre>
142- *
143- * @param filename the filename to retrieve the extension of.
144- * @return the extension of filename or an empty string if none exists.
145- */
146- public static String getExtension (String filename ) {
147- String suffix = "" ;
148- String shortFilename = filename ;
149- String ifilename = internalize (filename );
150-
151- int lastDirSeparator = ifilename .lastIndexOf (INTERNAL_SEPARATOR_CHAR );
152- if (lastDirSeparator > 0 ) {
153- shortFilename = ifilename .substring (lastDirSeparator + 1 );
154- }
155-
156- int index = shortFilename .lastIndexOf ('.' );
157-
158- if (index > 0 && index < shortFilename .length () - 1 ) {
159- suffix = shortFilename .substring (index + 1 );
160- }
161-
162- return suffix ;
163- }
164-
165- /**
166- * Remove path from filename. Equivalent to the unix command
167- * <code>basename</code>.
168- * ie.
169- * <pre>
170- * a/b/c.txt --> c.txt
171- * a.txt --> a.txt
172- * </pre>
173- *
174- * @param filepath the filepath
175- * @return the filename minus path
176- */
177- // KILL? Just use StringUtils?
178- public static String removePath (String filepath ) {
179- return removePath (filepath , File .separatorChar );
180- }
181-
182- /**
183- * Remove path from filename.
184- * ie.
185- * <pre>
186- * a/b/c.txt --> c.txt
187- * a.txt --> a.txt
188- * </pre>
189- *
190- * @param filepath the filepath
191- * @param fileSeparatorChar the file separator character to use
192- * @return the filename minus path
193- */
194- // KILL: Why allow the char to be specified?
195- public static String removePath ( String filepath , char fileSeparatorChar ) {
196- int index = filepath .lastIndexOf (fileSeparatorChar );
197-
198- if (-1 == index ) {
199- return filepath ;
200- } else {
201- return filepath .substring (index + 1 );
202- }
203- }
204-
205- /**
206- * Get path from filename. Roughly equivalent to the unix command
207- * <code>dirname</code>.
208- * ie.
209- * <pre>
210- * a/b/c.txt --> a/b
211- * a.txt --> ""
212- * </pre>
213- *
214- * @param filepath the filepath
215- * @return the filename minus path
216- */
217- // KILL? Just use StringUtils?
218- public static String getPath (String filepath ) {
219- return getPath (filepath , File .separatorChar );
220- }
221-
222- /**
223- * Get path from filename.
224- * ie.
225- * <pre>
226- * a/b/c.txt --> a/b
227- * a.txt --> ""
228- * </pre>
229- *
230- * @param filepath the filepath
231- * @param fileSeparatorChar the file separator character to use
232- * @return the filename minus path
233- */
234- // KILL: Why allow the char to be specified?
235- public static String getPath ( String filepath , char fileSeparatorChar ) {
236- int index = filepath .lastIndexOf (fileSeparatorChar );
237- if (-1 == index ) {
238- return "" ;
239- } else {
240- return filepath .substring (0 , index );
241- }
242- }
243-
244-
245-
246131 /**
247132 * Normalize a path.
248133 * Eliminates "/../" and "/./" in a string. Returns <code>null</code> if
@@ -500,16 +385,16 @@ public static String separatorsToSystem(String path) {
500385 * This method will handle a file in either Unix or Windows format.
501386 * The position of the last forward or backslash is returned.
502387 *
503- * @param path the path to find the last path separator in
388+ * @param filename the filename to find the last path separator in, null returns -1
504389 * @return the index of the last separator character, or -1 if there
505390 * is no such character.
506391 */
507- public static int indexOfLastSeparator (String path ) {
508- if (path == null ) {
392+ public static int indexOfLastSeparator (String filename ) {
393+ if (filename == null ) {
509394 return -1 ;
510395 }
511- int lastUnixPos = path .lastIndexOf (UNIX_SEPARATOR );
512- int lastWindowsPos = path .lastIndexOf (WINDOWS_SEPARATOR );
396+ int lastUnixPos = filename .lastIndexOf (UNIX_SEPARATOR );
397+ int lastWindowsPos = filename .lastIndexOf (WINDOWS_SEPARATOR );
513398 return Math .max (lastUnixPos , lastWindowsPos );
514399 }
515400
@@ -520,17 +405,97 @@ public static int indexOfLastSeparator(String path) {
520405 * To do this it uses {@link #indexOfLastSeparator(String)} which will
521406 * handle a file in either Unix or Windows format.
522407 *
523- * @param path the path to find the last path separator in
408+ * @param filename the filename to find the last path separator in, null returns -1
524409 * @return the index of the last separator character, or -1 if there
525410 * is no such character.
526411 */
527- public static int indexOfExtension (String path ) {
528- if (path == null ) {
412+ public static int indexOfExtension (String filename ) {
413+ if (filename == null ) {
529414 return -1 ;
530415 }
531- int extensionPos = path .lastIndexOf (EXTENSION_SEPARATOR );
532- int lastSeparator = indexOfLastSeparator (path );
416+ int extensionPos = filename .lastIndexOf (EXTENSION_SEPARATOR );
417+ int lastSeparator = indexOfLastSeparator (filename );
533418 return (lastSeparator > extensionPos ? -1 : extensionPos );
534419 }
535420
421+ //-----------------------------------------------------------------------
422+ /**
423+ * Gets the path from a full filename.
424+ * <p>
425+ * This method will handle a file in either Unix or Windows format.
426+ * The text before the last forward or backslash is returned.
427+ * This method is roughly equivalent to the unix command <code>dirname</code>.
428+ * <pre>
429+ * a/b/c.txt --> a/b
430+ * a.txt --> ""
431+ * a/b/c --> a/b
432+ * a/b/c/ --> a/b/c
433+ * </pre>
434+ *
435+ * @param filename the filename to query, null returns null
436+ * @return the filename minus path
437+ */
438+ public static String getPath (String filename ) {
439+ if (filename == null ) {
440+ return null ;
441+ }
442+ int index = indexOfLastSeparator (filename );
443+ if (index == -1 ) {
444+ return "" ;
445+ } else {
446+ return filename .substring (0 , index );
447+ }
448+ }
449+
450+ /**
451+ * Gets the name minus the path from a full filename.
452+ * <p>
453+ * This method will handle a file in either Unix or Windows format.
454+ * The text after the last forward or backslash is returned.
455+ * This method is roughly equivalent to the unix command <code>basename</code>.
456+ * <pre>
457+ * a/b/c.txt --> c.txt
458+ * a.txt --> a.txt
459+ * a/b/c --> c
460+ * a/b/c/ --> ""
461+ * </pre>
462+ *
463+ * @param filename the filename to query, null returns null
464+ * @return the filename minus path
465+ */
466+ public static String getName (String filename ) {
467+ if (filename == null ) {
468+ return null ;
469+ }
470+ int index = indexOfLastSeparator (filename );
471+ return filename .substring (index + 1 );
472+ }
473+
474+ /**
475+ * Gets the extension of a filename.
476+ * <p>
477+ * This method returns the textual part of the filename after the last dot.
478+ * There must be no directory separator after the dot.
479+ * <pre>
480+ * foo.txt --> "txt"
481+ * a/b/c.jpg --> "jpg"
482+ * a/b.txt/c --> ""
483+ * a/b/c --> ""
484+ * </pre>
485+ *
486+ * @param filename the filename to retrieve the extension of.
487+ * @return the extension of filename or an empty string if none exists.
488+ */
489+ public static String getExtension (String filename ) {
490+ if (filename == null ) {
491+ return null ;
492+ }
493+ int index = indexOfExtension (filename );
494+ if (index == -1 ) {
495+ return "" ;
496+ } else {
497+ return filename .substring (index + 1 );
498+ }
499+ }
500+
536501}
0 commit comments