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

Roger Riggs rriggs at openjdk.org
Fri Nov 3 19:31:02 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`.

Almost, except for the detail that both "photo" and "photo." have an empty string as the extension so the trailing ".", if any, should be dropped. 

Path p;
String name = p.getFileName().toString();
String ext = p.getExtension();
int index = name.length() - ext.length();
if (name.getChar(index - 1) == DOT)
    index--;
String s = name.substring(0, indexOfExt);
Path stripped = p.resolveSibling(s);

Keeping the `withoutExtension` method provides a place for that as a building block for `withExtension`.

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

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


More information about the nio-dev mailing list