Code cleanup: add getExtension and getName in FileUtils.java

This commit is contained in:
Jimmy Shih
2013-10-09 14:07:38 -07:00
parent 120c965e32
commit 2b633c091c
4 changed files with 36 additions and 23 deletions
@@ -144,6 +144,32 @@ public class FileUtils {
return buildUniqueFileName(directory, fileBaseName, extension, 0);
}
/**
* Gets the name from a file name, without the extension.
*
* @param fileName the file name
*/
public static String getName(String fileName) {
int index = fileName.lastIndexOf('.');
if (index == -1) {
return fileName;
}
return fileName.substring(0, index);
}
/**
* Gets the extension from a file name. Returns null if there is no extension.
*
* @param fileName the file name
*/
public static String getExtension(String fileName) {
int index = fileName.lastIndexOf('.');
if (index == -1) {
return null;
}
return fileName.substring(index + 1);
}
/**
* Builds a filename with the given base and the given extension, possibly
* adding a suffix to ensure the file doesn't exist.