RFR: 8057113: (fs) Path should have a method to obtain the filename extension [v3]

Brian Burkhalter bpb at openjdk.java.net
Wed Feb 24 22:53:01 UTC 2021


On Wed, 3 Feb 2021 17:50:17 GMT, Roger Riggs <rriggs at openjdk.org> wrote:

>> Using an Optional forces a user to read the javadoc (or worst guess), so i'm not sure it's a good idea to use it here because most people will just want the extension not a long speech about the filename being potentially null.
>> 
>> For me using "" when the extension doesn't exist whatever the reason is more pragmatic.
>
> Choice 3, the equivalent of getOrDefault, is compact and exposes explicitly the case where no extension is present with very little overhead.

Most use cases appear to concern switching on the extension to select among several alternatives:

- Get the file format name.
- Get an image reader to read a given image file.
- Get an icon to represent a given file in a file view.
- Select an application which can open a file of a certain type.

For this a string-based `getExtension()` method would seem to be enough. It is unclear how a platform representation would add any value or even whether one could be self-consistent given that an extension is not really an element in the sense of a root component, directory, or file. Each of these represents an actual component in the file system whereas an extension could be considered a sort of attribute.

Another related operation would be to remove the extension. A couple of uses of this are:

- Input file `archive.zip` unzips to `archive/`.
- Input file `archive.tar.gz` gunzips to `archive.tar`.

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

PR: https://git.openjdk.java.net/jdk/pull/2319


More information about the nio-dev mailing list