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 $
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final char
The extension separator character.static final String
The extension separator String.private static final char
The system separator character.private static final char
The Unix separator character.private static final char
The Windows separator character. -
Constructor Summary
ConstructorsConstructorDescriptionInstances should NOT be constructed in standard programming. -
Method Summary
Modifier and TypeMethodDescriptionstatic int
getPrefixLength
(String filename) Returns the length of the filename prefix, such asC:/
or~/
.private static boolean
isSeparator
(char ch) Checks if the character is a separator.(package private) static boolean
Determines if Windows file system is in use.
-
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
-
UNIX_SEPARATOR
private static final char UNIX_SEPARATORThe Unix separator character.- See Also:
-
WINDOWS_SEPARATOR
private static final char WINDOWS_SEPARATORThe Windows separator character.- See Also:
-
SYSTEM_SEPARATOR
private static final char SYSTEM_SEPARATORThe system separator character.
-
-
Constructor Details
-
FilenameUtils
public FilenameUtils()Instances should NOT be constructed in standard programming.
-
-
Method Details
-
isSystemWindows
static boolean isSystemWindows()Determines if Windows file system is in use.- Returns:
- true if the system is Windows
-
isSeparator
private static boolean isSeparator(char ch) Checks if the character is a separator.- Parameters:
ch
- the character to check- Returns:
- true if it is a separator character
-
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
-