Class FilenameUtils
- java.lang.Object
-
- org.zeroturnaround.zip.commons.FilenameUtils
-
public class FilenameUtils extends java.lang.Object
This is a class that has been made significantly smaller (deleted a bunch of methods) and originally is from the Apache Commons IO 2.2 package (the latest version that supports Java 1.5). All license and other documentation is intact. General filename and filepath manipulation utilities.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 constructorFile(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
Fields Modifier and Type Field Description static char
EXTENSION_SEPARATOR
The extension separator character.static java.lang.String
EXTENSION_SEPARATOR_STR
The extension separator String.private static char
SYSTEM_SEPARATOR
The system separator character.private static char
UNIX_SEPARATOR
The Unix separator character.private static char
WINDOWS_SEPARATOR
The Windows separator character.
-
Constructor Summary
Constructors Constructor Description FilenameUtils()
Instances should NOT be constructed in standard programming.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
getPrefixLength(java.lang.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
isSystemWindows()
Determines if Windows file system is in use.
-
-
-
Field Detail
-
EXTENSION_SEPARATOR
public static final char EXTENSION_SEPARATOR
The extension separator character.- Since:
- Commons IO 1.4
- See Also:
- Constant Field Values
-
EXTENSION_SEPARATOR_STR
public static final java.lang.String EXTENSION_SEPARATOR_STR
The extension separator String.- Since:
- Commons IO 1.4
-
UNIX_SEPARATOR
private static final char UNIX_SEPARATOR
The Unix separator character.- See Also:
- Constant Field Values
-
WINDOWS_SEPARATOR
private static final char WINDOWS_SEPARATOR
The Windows separator character.- See Also:
- Constant Field Values
-
SYSTEM_SEPARATOR
private static final char SYSTEM_SEPARATOR
The system separator character.
-
-
Method Detail
-
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
public static int getPrefixLength(java.lang.String filename)
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
-
-