下面列出了org.apache.commons.io.FilenameUtils#indexOfExtension ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Generate the conflict name of a file with help of the
* current time and the conflict-suffix. Consider the example
* hello_world.txt
*
* @param path to be renamed
* @return The renamed version of the provided path. Example:
* hello_world_CONFLICT_2015-03-02_18-03-17.txt
*/
public static Path rename(Path path) {
String pathString = path.toString();
String renamedFileString = null;
String conflictSuffix = "_CONFLICT_";
// get index of extension (even works with files like "hello.world.txt")
int indexOfExtension = FilenameUtils.indexOfExtension(pathString);
String fileName = pathString.substring(0, indexOfExtension);
String fileExtension = pathString.substring(indexOfExtension);
renamedFileString = fileName + conflictSuffix + currentDate() + fileExtension;
Path renamedFile = Paths.get(renamedFileString);
return renamedFile;
}
public static File save(File file, Object data, ResourceFileType type) throws IOException {
boolean hasExtension = FilenameUtils.indexOfExtension(file.getAbsolutePath()) != -1;
File output = hasExtension ? file : type.addExtensionIfMissing(file);
ensureDir(file);
getObjectMapper(type).writeValue(output, data);
return output;
}
@Override
public String pathWithoutExtension() {
int index = FilenameUtils.indexOfExtension(fileName);
if (index == -1) {
return fileName;
}
return fileName.substring(0, index);
}
private String[] splitFileName(final String fullPathName) {
final int extensionIndex;
String baseName = FilenameUtils.getBaseName(fullPathName);
if (fullPathName.endsWith(".gz") || fullPathName.endsWith(".zip")) {
extensionIndex = FilenameUtils.indexOfExtension(baseName);
baseName = fullPathName.substring(0, extensionIndex);
} else {
extensionIndex = FilenameUtils.indexOfExtension(fullPathName);
}
return new String[] { baseName, fullPathName.substring(extensionIndex)};
}
public static File[] getAllExactMatchingFiles(String sourceDir, String fileNamePrefix, String extension, List fileList, List skipList) {
File libDir = new File(sourceDir);
String libDirPath = libDir.getAbsolutePath();
File[] items = libDir.listFiles();
if (items != null) {
for (int i = 0; i < items.length; i++) {
if(items[i].isDirectory()){
if(!skipList.contains(items[i].getName())){
getAllExactMatchingFiles(items[i].getPath(), fileNamePrefix, extension, fileList,skipList);
}
}else{
String item = items[i].getName();
String fileName = null;
String ext = "";
if(FilenameUtils.indexOfExtension(item)==-1){
fileName=item;
}else{
fileName = FilenameUtils.removeExtension(item);
ext = FilenameUtils.getExtension(item);
}
if (fileNamePrefix != null && extension != null) {
if (fileNamePrefix.equalsIgnoreCase(fileName) && extension.equalsIgnoreCase(ext)) {
fileList.add(new File(libDirPath + File.separator + item));
}
} else if (fileNamePrefix == null && extension != null) {
if (extension.equalsIgnoreCase(ext)) {
fileList.add(new File(libDirPath + File.separator + item));
}
} else if (fileNamePrefix != null && extension == null) {
if (fileNamePrefix.equalsIgnoreCase(fileName)) {
fileList.add(new File(libDirPath + File.separator + item));
}
} else {
fileList.add(new File(libDirPath + File.separator + item));
}
}
}
return (File[]) fileList.toArray(new File[fileList.size()]);
}
return new File[0];
}