/** * 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(final String filename) { return doGetFullPath(filename, false); }
/** * 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(final String filename) { return doGetPath(filename, 0); }
return doNormalize(filename, SYSTEM_SEPARATOR, false);
/** * 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(final String filename) { return removeExtension(getName(filename)); }
/** * Converts all separators to the system separator. * * @param path the path to be changed, null ignored * @return the updated path */ public static String separatorsToSystem(final String path) { if (path == null) { return null; } if (isSystemWindows()) { return separatorsToWindows(path); } else { return separatorsToUnix(path); } }
final int prefix = getPrefixLength(fullFilenameToAdd); if (prefix < 0) return normalize(fullFilenameToAdd); if (len == 0) return normalize(fullFilenameToAdd); if (isSeparator(ch)) return normalize(basePath + fullFilenameToAdd); return normalize(basePath + '/' + fullFilenameToAdd);
final int prefix = getPrefixLength(filename); if (prefix < 0) return getPrefix(filename); // add end slash if necessary final int index = indexOfLastSeparator(filename); if (index < 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(final String filename, final int separatorAdd) { if (filename == null) { return null; } final int prefix = getPrefixLength(filename); if (prefix < 0) { return null; } final int index = indexOfLastSeparator(filename); final int endIndex = index + separatorAdd; if (prefix >= filename.length() || index < 0 || prefix >= endIndex) { return ""; } return filename.substring(prefix, endIndex); }
/** * Returns the index of the last extension separator character, which is a dot. * <p> * This method also checks that there is no directory separator after the last dot. * To do this it uses {@link #indexOfLastSeparator(String)} which will * handle a file in either Unix or Windows format. * <p> * The output will be the same irrespective of the machine that the code is running on. * * @param filename the filename to find the last path separator in, null returns -1 * @return the index of the last separator character, or -1 if there * is no such character */ public static int indexOfExtension(final String filename) { if (filename == null) { return -1; } final int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR); final int lastSeparator = indexOfLastSeparator(filename); return lastSeparator > extensionPos ? -1 : extensionPos; }
final int index = indexOfExtension(filename); if (index == -1)
final int len = getPrefixLength(filename); if (len < 0)
return isSeparator(ch0) ? 1 : 0; if (ch0 >= 'A' && ch0 <= 'Z') if (len == 2 || isSeparator(filename.charAt(2)) == false) else if (isSeparator(ch0) && isSeparator(ch1)) return isSeparator(ch0) ? 1 : 0;
/** * 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(final String filename) { if (filename == null) { return null; } final int index = indexOfLastSeparator(filename); return filename.substring(index + 1); }
final int index = indexOfExtension(filename); if (index == -1)
final int prefix = getPrefixLength(filename); if (prefix < 0)
return doNormalize(filename, SYSTEM_SEPARATOR, true);
/** * Gets the path from a full filename, which excludes the prefix. * <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 --> 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(final String filename) { return doGetPath(filename, 1); }
/** * 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(final String filename) { return doGetFullPath(filename, true); }
return doNormalize(filename, separator, true);
return doNormalize(filename, separator, false);