Java File Extension Name Extract extractFileExtensionFromPath(String path)

Here you can find the source of extractFileExtensionFromPath(String path)

Description

Extracts file's extension in ".extension" format.

License

Open Source License

Parameter

Parameter Description
path the file path

Exception

Parameter Description
IllegalArgumentException if file path is empty
NullPointerException if file path is null

Return

the extension in lowercase,
null if file has no extension

Declaration

public static String extractFileExtensionFromPath(String path) 

Method Source Code

//package com.java2s;
/** /* www.  ja v a 2 s . c o m*/
 * StreamSis
 * Copyright (C) 2015 Eva Balycheva
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Extracts file's extension in ".extension" format. <br>
     *
     * @param path
     *            the file path
     * @return the extension in lowercase,<br>
     *         null if file has no extension
     * @throws IllegalArgumentException
     *             if file path is empty
     * @throws NullPointerException
     *             if file path is null
     */
    public static String extractFileExtensionFromPath(String path) {
        if (path != null) {
            if (path.isEmpty()) {
                throw new IllegalArgumentException("File path can't be empty");
            }
        } else {
            throw new NullPointerException("File path can't be null");
        }
        String lowercaseName = path.toLowerCase();
        int lastIndexOfDot = lowercaseName.lastIndexOf(".");
        if (lastIndexOfDot == -1) {
            return null;
        }
        return lowercaseName.substring(lastIndexOfDot);
    }
}

Related

  1. extractFileExtension(String filename)
  2. extractFileExtension(String filename)
  3. extractFileExtension(String fileName)
  4. extractFileExtension(String filePath)
  5. extractFileExtension(String path)
  6. getFileExtension(File f)
  7. getFileExtension(File file)
  8. getFileExtension(File file)
  9. getFileExtension(File file)