public class FilenameUtils extends Object
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):
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.
Modifier and Type | Field and Description |
---|---|
static char |
EXTENSION_SEPARATOR
The extension separator character.
|
static String |
EXTENSION_SEPARATOR_STR
The extension separator String.
|
Constructor and Description |
---|
FilenameUtils()
Instances should NOT be constructed in standard programming.
|
Modifier and Type | Method and Description |
---|---|
static int |
getPrefixLength(String filename)
Returns the length of the filename prefix, such as
C:/ or ~/ . |
public static final char EXTENSION_SEPARATOR
public static final String EXTENSION_SEPARATOR_STR
public FilenameUtils()
public static int getPrefixLength(String filename)
C:/
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.
filename
- the filename to find the prefix in, null returns -1Copyright © 2017 ZeroTurnaround. All rights reserved.