RFR: 8298318: (fs) APIs for handling filename extensions

Roger Riggs rriggs at openjdk.org
Wed Oct 18 14:24:00 UTC 2023


On Tue, 17 Oct 2023 19:52:14 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

> Add to `java.nio.file.Path` a method `getExtension` to retrieve the `Path`'s extension, and companion methods `removeExtension` and `addExtension`.

There are a few builder style APIs for immutable objects that have `withXXX` methods that return an instance with the change requested. A `withExtension(String ext)` could handle the common cases for removing, replacing, and adding an extension.

Path p = Path.of("photo.png");
Path base = p.withExtension("") -> "photo" // replaces any extension with the empty extension (and no dot)
p.withExtension("jpg") -> "photo.jpg"      // replaces the extension
base.withExtension("pdf") -> "photo.pdf"   // adds the extension

Removing an extension with `""` may be a bit unusual, perhaps an additional method  would aid discoverablity`basepath()` or `dropExtension`, or `trimExtension`.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/16226#issuecomment-1768573625


More information about the nio-dev mailing list