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);
}

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License