Class FilenameUtils
When dealing with filenames you can hit problems when moving from a Windows based development machine to a Unix based production machine. This class aims to help avoid those problems.
NOTE: You may be able to avoid using this class entirely simply by using JDK File objects and the two argument constructor
File(File,String).
Most methods on this class are designed to work the same on both Unix and Windows. Those that don't include 'System', 'Unix' or 'Windows' in their name.
Most methods recognise both separators (forward and back), and both sets of prefixes. See the javadoc of each method for details.
This class defines six components within a filename (example C:\dev\project\file.txt):
- the prefix - C:\
- the path - dev\project\
- the full path - C:\dev\project\
- the name - file.txt
- the base name - file
- the extension - txt
This class only supports Unix and Windows style names. Prefixes are matched as follows:
Windows: a\b\c.txt --> "" --> relative \a\b\c.txt --> "\" --> current drive absolute C:a\b\c.txt --> "C:" --> drive relative C:\a\b\c.txt --> "C:\" --> absolute \\server\a\b\c.txt --> "\\server\" --> UNC Unix: a/b/c.txt --> "" --> relative /a/b/c.txt --> "/" --> absolute ~/a/b/c.txt --> "~/" --> current user ~ --> "~/" --> current user (slash added) ~user/a/b/c.txt --> "~user/" --> named user ~user --> "~user/" --> named user (slash added)Both prefix styles are matched always, irrespective of the machine that you are currently running on.
Origin of code: Excalibur, Alexandria, Tomcat, Commons-Utils.
- Since:
- Commons IO 1.1
- Version:
- $Id: FilenameUtils.java 609870 2008-01-08 04:46:26Z niallp $
- Author:
- Kevin A. Burton, Scott Sanders, Daniel Rall, Christoph.Reck, Peter Donald, Jeff Turner, Matthew Hawthorne, Martin Cooper, Jeremias Maerki, Stephen Colebourne
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final charThe extension separator character.static final StringThe extension separator String. -
Constructor Summary
ConstructorsConstructorDescriptionInstances should NOT be constructed in standard programming. -
Method Summary
Modifier and TypeMethodDescriptionstatic intgetPrefixLength(String filename) Returns the length of the filename prefix, such asC:/or~/.
-
Field Details
-
EXTENSION_SEPARATOR
public static final char EXTENSION_SEPARATORThe extension separator character.- Since:
- Commons IO 1.4
- See Also:
-
EXTENSION_SEPARATOR_STR
The extension separator String.- Since:
- Commons IO 1.4
-
-
Constructor Details
-
FilenameUtils
public FilenameUtils()Instances should NOT be constructed in standard programming.
-
-
Method Details
-
getPrefixLength
Returns the length of the filename prefix, such asC:/or~/.This method will handle a file in either Unix or Windows format.
The prefix length includes the first slash in the full filename if applicable. Thus, it is possible that the length returned is greater than the length of the input string.
Windows: a\b\c.txt --> "" --> relative \a\b\c.txt --> "\" --> current drive absolute C:a\b\c.txt --> "C:" --> drive relative C:\a\b\c.txt --> "C:\" --> absolute \\server\a\b\c.txt --> "\\server\" --> UNC Unix: a/b/c.txt --> "" --> relative /a/b/c.txt --> "/" --> absolute ~/a/b/c.txt --> "~/" --> current user ~ --> "~/" --> current user (slash added) ~user/a/b/c.txt --> "~user/" --> named user ~user --> "~user/" --> named user (slash added)
The output will be the same irrespective of the machine that the code is running on. ie. both Unix and Windows prefixes are matched regardless.
- Parameters:
filename- the filename to find the prefix in, null returns -1- Returns:
- the length of the prefix, -1 if invalid or null
-