Setup
private String FILE_PATH = "com/levelup/java/io/sample.md";
Straight up Java
@Test
public void get_file_name_with_out_extension_java () {
String separator = System.getProperty("file.separator");
int indexOfLastSeparator = FILE_PATH.lastIndexOf(separator);
String filename = FILE_PATH.substring(indexOfLastSeparator + 1);
int extensionIndex = filename.lastIndexOf(".");
String fileExtension = filename.substring(0, extensionIndex);
assertEquals("sample", fileExtension);
}
Google Guava
@Test
public void get_file_name_with_out_extension_guava () {
String fileName = Files.getNameWithoutExtension(FILE_PATH);
assertEquals("sample", fileName);
}
Apache Commons
@Test
public void get_file_name_with_out_extension_apache () {
String fileName = FilenameUtils.getBaseName(FILE_PATH);
assertEquals("sample", fileName);
}