?? filenameutils.java
字號:
* The method is entirely text based, and returns the text before and
* including the last forward or backslash.
* <pre>
* C:\a\b\c.txt --> a\b\
* ~/a/b/c.txt --> a/b/
* a.txt --> ""
* a/b/c --> a/b/
* a/b/c/ --> a/b/c/
* </pre>
* <p>
* The output will be the same irrespective of the machine that the code is running on.
* <p>
* This method drops the prefix from the result.
* See {@link #getFullPath(String)} for the method that retains the prefix.
*
* @param filename the filename to query, null returns null
* @return the path of the file, an empty string if none exists, null if invalid
*/
public static String getPath(String filename) {
return doGetPath(filename, 1);
}
/**
* Gets the path from a full filename, which excludes the prefix, and
* also excluding the final directory separator.
* <p>
* This method will handle a file in either Unix or Windows format.
* The method is entirely text based, and returns the text before the
* last forward or backslash.
* <pre>
* C:\a\b\c.txt --> a\b
* ~/a/b/c.txt --> a/b
* a.txt --> ""
* a/b/c --> a/b
* a/b/c/ --> a/b/c
* </pre>
* <p>
* The output will be the same irrespective of the machine that the code is running on.
* <p>
* This method drops the prefix from the result.
* See {@link #getFullPathNoEndSeparator(String)} for the method that retains the prefix.
*
* @param filename the filename to query, null returns null
* @return the path of the file, an empty string if none exists, null if invalid
*/
public static String getPathNoEndSeparator(String filename) {
return doGetPath(filename, 0);
}
/**
* Does the work of getting the path.
*
* @param filename the filename
* @param separatorAdd 0 to omit the end separator, 1 to return it
* @return the path
*/
private static String doGetPath(String filename, int separatorAdd) {
if (filename == null) {
return null;
}
int prefix = getPrefixLength(filename);
if (prefix < 0) {
return null;
}
int index = indexOfLastSeparator(filename);
if (prefix >= filename.length() || index < 0) {
return "";
}
return filename.substring(prefix, index + separatorAdd);
}
/**
* Gets the full path from a full filename, which is the prefix + path.
* <p>
* This method will handle a file in either Unix or Windows format.
* The method is entirely text based, and returns the text before and
* including the last forward or backslash.
* <pre>
* C:\a\b\c.txt --> C:\a\b\
* ~/a/b/c.txt --> ~/a/b/
* a.txt --> ""
* a/b/c --> a/b/
* a/b/c/ --> a/b/c/
* C: --> C:
* C:\ --> C:\
* ~ --> ~/
* ~/ --> ~/
* ~user --> ~user/
* ~user/ --> ~user/
* </pre>
* <p>
* The output will be the same irrespective of the machine that the code is running on.
*
* @param filename the filename to query, null returns null
* @return the path of the file, an empty string if none exists, null if invalid
*/
public static String getFullPath(String filename) {
return doGetFullPath(filename, true);
}
/**
* Gets the full path from a full filename, which is the prefix + path,
* and also excluding the final directory separator.
* <p>
* This method will handle a file in either Unix or Windows format.
* The method is entirely text based, and returns the text before the
* last forward or backslash.
* <pre>
* C:\a\b\c.txt --> C:\a\b
* ~/a/b/c.txt --> ~/a/b
* a.txt --> ""
* a/b/c --> a/b
* a/b/c/ --> a/b/c
* C: --> C:
* C:\ --> C:\
* ~ --> ~
* ~/ --> ~
* ~user --> ~user
* ~user/ --> ~user
* </pre>
* <p>
* The output will be the same irrespective of the machine that the code is running on.
*
* @param filename the filename to query, null returns null
* @return the path of the file, an empty string if none exists, null if invalid
*/
public static String getFullPathNoEndSeparator(String filename) {
return doGetFullPath(filename, false);
}
/**
* Does the work of getting the path.
*
* @param filename the filename
* @param includeSeparator true to include the end separator
* @return the path
*/
private static String doGetFullPath(String filename, boolean includeSeparator) {
if (filename == null) {
return null;
}
int prefix = getPrefixLength(filename);
if (prefix < 0) {
return null;
}
if (prefix >= filename.length()) {
if (includeSeparator) {
return getPrefix(filename); // add end slash if necessary
} else {
return filename;
}
}
int index = indexOfLastSeparator(filename);
if (index < 0) {
return filename.substring(0, prefix);
}
int end = index + (includeSeparator ? 1 : 0);
return filename.substring(0, end);
}
/**
* Gets the name minus the path from a full filename.
* <p>
* This method will handle a file in either Unix or Windows format.
* The text after the last forward or backslash is returned.
* <pre>
* a/b/c.txt --> c.txt
* a.txt --> a.txt
* a/b/c --> c
* a/b/c/ --> ""
* </pre>
* <p>
* The output will be the same irrespective of the machine that the code is running on.
*
* @param filename the filename to query, null returns null
* @return the name of the file without the path, or an empty string if none exists
*/
public static String getName(String filename) {
if (filename == null) {
return null;
}
int index = indexOfLastSeparator(filename);
return filename.substring(index + 1);
}
/**
* Gets the base name, minus the full path and extension, from a full filename.
* <p>
* This method will handle a file in either Unix or Windows format.
* The text after the last forward or backslash and before the last dot is returned.
* <pre>
* a/b/c.txt --> c
* a.txt --> a
* a/b/c --> c
* a/b/c/ --> ""
* </pre>
* <p>
* The output will be the same irrespective of the machine that the code is running on.
*
* @param filename the filename to query, null returns null
* @return the name of the file without the path, or an empty string if none exists
*/
public static String getBaseName(String filename) {
return removeExtension(getName(filename));
}
/**
* Gets the extension of a filename.
* <p>
* This method returns the textual part of the filename after the last dot.
* There must be no directory separator after the dot.
* <pre>
* foo.txt --> "txt"
* a/b/c.jpg --> "jpg"
* a/b.txt/c --> ""
* a/b/c --> ""
* </pre>
* <p>
* The output will be the same irrespective of the machine that the code is running on.
*
* @param filename the filename to retrieve the extension of.
* @return the extension of the file or an empty string if none exists.
*/
public static String getExtension(String filename) {
if (filename == null) {
return null;
}
int index = indexOfExtension(filename);
if (index == -1) {
return "";
} else {
return filename.substring(index + 1);
}
}
//-----------------------------------------------------------------------
/**
* Removes the extension from a filename.
* <p>
* This method returns the textual part of the filename before the last dot.
* There must be no directory separator after the dot.
* <pre>
* foo.txt --> foo
* a\b\c.jpg --> a\b\c
* a\b\c --> a\b\c
* a.b\c --> a.b\c
* </pre>
* <p>
* The output will be the same irrespective of the machine that the code is running on.
*
* @param filename the filename to query, null returns null
* @return the filename minus the extension
*/
public static String removeExtension(String filename) {
if (filename == null) {
return null;
}
int index = indexOfExtension(filename);
if (index == -1) {
return filename;
} else {
return filename.substring(0, index);
}
}
//-----------------------------------------------------------------------
/**
* Checks whether two filenames are equal exactly.
* <p>
* No processing is performed on the filenames other than comparison,
* thus this is merely a null-safe case-sensitive equals.
*
* @param filename1 the first filename to query, may be null
* @param filename2 the second filename to query, may be null
* @return true if the filenames are equal, null equals null
*/
public static boolean equals(String filename1, String filename2) {
return equals(filename1, filename2, false, false);
}
/**
* Checks whether two filenames are equal using the case rules of the system.
* <p>
* No processing is performed on the filenames other than comparison.
* The check is case-sensitive on Unix and case-insensitive on Windows.
*
* @param filename1 the first filename to query, may be null
* @param filename2 the second filename to query, may be null
* @return true if the filenames are equal, null equals null
*/
public static boolean equalsOnSystem(String filename1, String filename2) {
return equals(filename1, filename2, true, false);
}
//-----------------------------------------------------------------------
/**
* Checks whether two filenames are equal after both have been normalized.
* <p>
* Both filenames are first passed to {@link #normalize(String)}.
* The check is then performed in a case-sensitive manner.
*
* @param filename1 the first filename to query, may be null
* @param filename2 the second filename to query, may be null
* @return true if the filenames are equal, null equals null
*/
public static boolean equalsNormalized(String filename1, String filename2) {
return equals(filename1, filename2, false, true);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -