Java Path File Extention nio getFileExtension(Path path)

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

Description

Gets the file extension of the given path.

License

Open Source License

Parameter

Parameter Description
path the path.

Exception

Parameter Description
NullPointerException if path is null or has zero elements.

Return

the extension, with leading dot (".").

Declaration

public static String getFileExtension(Path path) 

Method Source Code

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

import java.nio.file.Path;

public class Main {
    /**/*  w w w .  j  a va2 s.  co  m*/
     * Gets the file extension of the given path. The extension begins at the last occurrence of ".". Returns an empty
     * string if the file does not contain a dot (".") or begins with one (hidden file in unix).
     *
     * @param path the path.
     * @return the extension, with leading dot (".").
     * @throws NullPointerException if path is null or has zero elements.
     * @see Path#getFileName()
     */
    public static String getFileExtension(Path path) {
        String ext = path.getFileName().toString();
        int index = ext.lastIndexOf('.');
        if (index <= 0) {
            return "";
        } else {
            return ext.substring(index, ext.length());
        }
    }
}

Related

  1. getFileExtension(final Path file)
  2. getFileExtension(final Path path)
  3. getFileExtension(Path file)
  4. getFileextension(Path path)
  5. getFileExtension(Path path)
  6. getFileExtension(Path path)
  7. getFileExtension(Path path)
  8. getFileNameNoExtensionFromPath(String modulePath)
  9. getFilenameWithoutExtension(Path file)