adam bien's blog

Java 11: Extract File Name from Path 📎

An alternative to lastIndexOf / substring combination to extract the file name from the path:


import java.nio.file.Path;

@Test
public void extractFileName() {
    var expected = "duke.html";
    var fqn = "/tmp/another/" + expected;
    
    var actual = Path.of(fqn).getFileName().toString();
    
    assertEquals(expected, actual);
}