本文整理了Java中org.apache.commons.io.FilenameUtils.indexOfLastSeparator()
方法的一些代码示例,展示了FilenameUtils.indexOfLastSeparator()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FilenameUtils.indexOfLastSeparator()
方法的具体详情如下:
包路径:org.apache.commons.io.FilenameUtils
类名称:FilenameUtils
方法名:indexOfLastSeparator
[英]Returns the index of the last directory separator character.
This method will handle a file in either Unix or Windows format. The position of the last forward or backslash is returned.
The output will be the same irrespective of the machine that the code is running on.
[中]返回最后一个目录分隔符字符的索引。
此方法将处理Unix或Windows格式的文件。返回最后一个向前或向后斜杠的位置。
无论代码在哪台机器上运行,输出都是相同的。
代码示例来源:origin: commons-io/commons-io
/**
* 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>
* <p>
* The output will be the same irrespective of the machine that the code is running on.
* </p>
*
* @param filename
* the filename to find the last extension separator in, null returns -1
* @return the index of the last extension separator character, or -1 if there is no such character
*/
public static int indexOfExtension(final String filename) {
if (filename == null) {
return NOT_FOUND;
}
final int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR);
final int lastSeparator = indexOfLastSeparator(filename);
return lastSeparator > extensionPos ? NOT_FOUND : extensionPos;
}
代码示例来源:origin: commons-io/commons-io
/**
* 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.
* Null bytes inside string will be removed
*/
public static String getName(final String filename) {
if (filename == null) {
return null;
}
failIfNullBytePresent(filename);
final int index = indexOfLastSeparator(filename);
return filename.substring(index + 1);
}
代码示例来源:origin: org.apache.commons/commons-io
/**
* 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(String filename) {
if (filename == null) {
return -1;
}
int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR);
int lastSeparator = indexOfLastSeparator(filename);
return (lastSeparator > extensionPos ? -1 : extensionPos);
}
代码示例来源:origin: org.apache.commons/commons-io
/**
* 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);
}
代码示例来源:origin: commons-io/commons-io
/**
* 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. Null bytes inside string will be removed
*/
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 "";
}
final String path = filename.substring(prefix, endIndex);
failIfNullBytePresent(path);
return path;
}
代码示例来源:origin: commons-io/commons-io
final int index = indexOfLastSeparator(filename);
if (index < 0) {
return filename.substring(0, prefix);
代码示例来源:origin: org.apache.commons/commons-io
/**
* 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);
}
代码示例来源:origin: commons-io/commons-io
@Test
public void testIndexOfLastSeparator() {
assertEquals(-1, FilenameUtils.indexOfLastSeparator(null));
assertEquals(-1, FilenameUtils.indexOfLastSeparator("noseperator.inthispath"));
assertEquals(3, FilenameUtils.indexOfLastSeparator("a/b/c"));
assertEquals(3, FilenameUtils.indexOfLastSeparator("a\\b\\c"));
}
代码示例来源:origin: org.apache.commons/commons-io
/**
* 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);
}
代码示例来源:origin: neuland/jade4j
public String resolvePath(String parentName, String templateName, String basePath, String extension) {
// Path currentPath = Paths.get(filename);
// Path templatePath = Paths.get(templateName);
// Path parent = currentPath.getParent();
// String filePath = templatePath.toString();
// if(parent!=null)
// filePath = parent.resolve(templatePath).toString();
String filePath;
if(templateName.startsWith("/")) {
filePath = basePath + templateName;
}else {
if (FilenameUtils.indexOfLastSeparator(parentName) == -1)
filePath = templateName;
else {
// String currentDir = FilenameUtils.getFullPath(parentName);
String currentDir = parentName.substring(0, FilenameUtils.indexOfLastSeparator(parentName) + 1);
filePath = currentDir + templateName;
}
}
if(StringUtils.lastIndexOf(filePath,"/") >= StringUtils.lastIndexOf(filePath,"."))
filePath += "."+extension;
filePath = FilenameUtils.normalize(filePath);
return filePath;
}
}
代码示例来源:origin: pentaho/pentaho-platform
/**
* Returns the index of the last directory separator character.
* <p/>
* The position of the last forward or backslash is returned.
* <p/>
*
* @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 indexOfLastSeparator( final String filename ) {
return FilenameUtils.indexOfLastSeparator( filename );
}
代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.commons-io
/**
* 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(String filename) {
if (filename == null) {
return -1;
}
int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR);
int lastSeparator = indexOfLastSeparator(filename);
return (lastSeparator > extensionPos ? -1 : extensionPos);
}
代码示例来源:origin: org.onosproject/onlab-thirdparty
/**
* 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(String filename) {
if (filename == null) {
return -1;
}
int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR);
int lastSeparator = indexOfLastSeparator(filename);
return lastSeparator > extensionPos ? -1 : extensionPos;
}
代码示例来源:origin: org.uberfire/vfs-model
/**
* 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(String filename) {
if (filename == null) {
return -1;
}
int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR);
int lastSeparator = indexOfLastSeparator(filename);
return lastSeparator > extensionPos ? -1 : extensionPos;
}
代码示例来源:origin: org.nuiton.js/nuiton-js-wro
/**
* This method fixes the problem when a resource in a group uses deep wildcard and starts at the root.
* <p/>
* Find more details <a href="https://github.com/alexo/wro4j/pull/44">here</a>.
*/
private String getFullPathNoEndSeparator(final String uri) {
String result = FilenameUtils.getFullPathNoEndSeparator(uri);
if (result != null && 1 == result.length() && 0 == FilenameUtils.indexOfLastSeparator(result))
return "";
return result;
}
};
代码示例来源:origin: digital-preservation/droid
/**
*
* @param path The path of a directory
* @return String[] a string array containing the parent folders, each of
* which is a path in its own right (not just the names of each individual folder)
*/
public static List<String> getAncestorPaths(String path) {
ArrayList<String> paths = new ArrayList<String>();
if (path != null && !path.isEmpty()) {
String processPath = path;
int lastSeparator = processPath.length() - 1;
while (lastSeparator >= 0) {
String separator = path.substring(lastSeparator, lastSeparator + 1);
processPath = processPath.substring(0, lastSeparator);
paths.add(processPath + separator);
lastSeparator = FilenameUtils.indexOfLastSeparator(processPath);
}
}
return paths;
}
代码示例来源:origin: org.uberfire/vfs-model
/**
* 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);
int endIndex = index+separatorAdd;
if (prefix >= filename.length() || index < 0 || prefix >= endIndex) {
return "";
}
return filename.substring(prefix, endIndex);
}
代码示例来源:origin: org.onosproject/onlab-thirdparty
/**
* 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);
int endIndex = index+separatorAdd;
if (prefix >= filename.length() || index < 0 || prefix >= endIndex) {
return "";
}
return filename.substring(prefix, endIndex);
}
代码示例来源:origin: ro.isdc.wro4j/wro4j-core
/**
* This method fixes the problem when a resource in a group uses deep wildcard and starts at the root.
* <p/>
* Find more details <a href="https://github.com/alexo/wro4j/pull/44">here</a>.
*/
private String getFullPathNoEndSeparator(final Resource resource1) {
final String result = FilenameUtils.getFullPathNoEndSeparator(resource1.getUri());
if (result != null && 1 == result.length() && 0 == FilenameUtils.indexOfLastSeparator(result)) {
return "";
}
return result;
}
};
代码示例来源:origin: alexo/wro4j
/**
* This method fixes the problem when a resource in a group uses deep wildcard and starts at the root.
* <p/>
* Find more details <a href="https://github.com/alexo/wro4j/pull/44">here</a>.
*/
private String getFullPathNoEndSeparator(final Resource resource) {
final String result = FilenameUtils.getFullPathNoEndSeparator(resource.getUri());
if (result != null && 1 == result.length() && 0 == FilenameUtils.indexOfLastSeparator(result)) {
return "";
}
return result;
}
};
内容来源于网络,如有侵权,请联系作者删除!