Java Path File Extention nio getFileExtension(Path file)

Here you can find the source of getFileExtension(Path file)

Description

Returns the extension of a file or Optional.empty() if the file does not have one (no .

License

Open Source License

Parameter

Parameter Description
file a parameter

Return

The extension, trimmed and in lowercase.

Declaration

public static Optional<String> getFileExtension(Path file) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.nio.file.Path;

import java.util.Locale;

import java.util.Optional;

public class Main {
    /**/*w  ww. jav a  2 s .c  o m*/
     * Returns the extension of a file or Optional.empty() if the file does not have one (no . in name).
     *
     * @param file
     * @return The extension, trimmed and in lowercase.
     */
    public static Optional<String> getFileExtension(Path file) {
        return getFileExtension(file.toString());
    }

    /**
     * Returns the extension of a file name or Optional.empty() if the file does not have one (no "." in name).
     *
     * @param fileName
     * @return The extension (without leading dot), trimmed and in lowercase.
     */
    public static Optional<String> getFileExtension(String fileName) {
        int dotPosition = fileName.lastIndexOf('.');
        if ((dotPosition > 0) && (dotPosition < (fileName.length() - 1))) {
            return Optional.of(fileName.substring(dotPosition + 1).trim().toLowerCase(Locale.ROOT));
        } else {
            return Optional.empty();
        }
    }
}

Related

  1. getExtension(final Path path)
  2. getExtension(Path file)
  3. getExtension(Path path)
  4. getFileExtension(final Path file)
  5. getFileExtension(final Path path)
  6. getFileextension(Path path)
  7. getFileExtension(Path path)
  8. getFileExtension(Path path)
  9. getFileExtension(Path path)