Here you can find the source of getFileExtension(Path file)
Parameter | Description |
---|---|
file | a parameter |
public static Optional<String> getFileExtension(Path file)
//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(); } } }