Here you can find the source of extractFileExtensionFromPath(String path)
Parameter | Description |
---|---|
path | the file path |
Parameter | Description |
---|---|
IllegalArgumentException | if file path is empty |
NullPointerException | if file path is null |
public static String extractFileExtensionFromPath(String path)
//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); } }