RFR 8057113: (fs) Path should have a method to test the filename extension

David Lloyd david.lloyd at redhat.com
Fri Feb 16 14:27:34 UTC 2018


On Fri, Feb 16, 2018 at 8:26 AM, David Lloyd <david.lloyd at redhat.com> wrote:
> On Thu, Feb 15, 2018 at 8:53 PM, Brian Burkhalter
> <brian.burkhalter at oracle.com> wrote:
>> Here is an updated version which changes the specification and
>> implementation to define things in terms of the file name element instead of
>> the entire Path.
>
> +    default String getExtension() {
> +        Path fname = getFileName();
> +        if (fname == null) {
> +            return "";
> +        }
> +
> +        String name = fname.toString();
> +        int lastDot = name.lastIndexOf(".");
> +        if (lastDot == -1 || lastDot == name.length() - 1) {
> +            return "";
> +        }
> +
> +        return name.substring(lastDot + 1);
> +    }
>
> If lastDot is 0 or all the characters preceding lastDot are '.' then
> "" should be returned as well; this for example will not only return
> wrong results for ".." but also e.g. ".vimrc".

Also you could do "name.lastIndexOf('.')" instead.

-- 
- DML


More information about the nio-dev mailing list