Get file extension

Setup

private String FILE_PATH = "com/levelup/java/io/sample.log";

Straight up Java

@Test
public void get_file_extension_with_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(extensionIndex + 1);
    
    assertEquals("log", fileExtension);
}

Google Guava

@Test
public void get_file_extension_with_guava () {
    
    String fileExtension = Files.getFileExtension(FILE_PATH);
    assertEquals("log", fileExtension);
}

Apache Commons

@Test
public void get_file_extension_with_apache_commons () {
    
    String fileExtension = FilenameUtils.getExtension(FILE_PATH);
    assertEquals("log", fileExtension);
}