Here you can find the source of getExtension(Path path)
Parameter | Description |
---|---|
path | The path to operate on. |
public static String getExtension(Path path)
//package com.java2s; /*/* www.jav a2s . co m*/ * Copyright (c) 1998-2017 by Richard A. Wilkes. All rights reserved. * * This Source Code Form is subject to the terms of the Mozilla Public * License, version 2.0. If a copy of the MPL was not distributed with * this file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This Source Code Form is "Incompatible With Secondary Licenses", as * defined by the Mozilla Public License, version 2.0. */ import java.nio.file.Path; public class Main { /** * @param path The path to operate on. * @return The extension of the path name, excluding the initial ".". */ public static String getExtension(Path path) { return getExtension(path != null ? path.toString() : null); } /** * @param path The path to operate on. * @return The extension of the path name, excluding the initial ".". */ public static final String getExtension(String path) { path = getLeafName(path); if (path != null) { int dot = path.lastIndexOf('.'); if (dot != -1 && dot + 1 < path.length()) { return path.substring(dot + 1); } } return ""; } /** * @param path The path to process. * @return The leaf portion of the path name (everything to the right of the last path * separator). */ public static final String getLeafName(String path) { return getLeafName(path, true); } /** * @param path The path to process. * @param includeExtension Pass in <code>true</code> to leave the extension on the name or * <code>false</code> to strip it off. * @return The leaf portion of the path name (everything to the right of the last path * separator). */ public static final String getLeafName(Path path, boolean includeExtension) { return getLeafName(path.toString(), includeExtension); } /** * @param path The path to process. * @param includeExtension Pass in <code>true</code> to leave the extension on the name or * <code>false</code> to strip it off. * @return The leaf portion of the path name (everything to the right of the last path * separator). */ public static final String getLeafName(String path, boolean includeExtension) { if (path != null) { int index; path = path.replace('\\', '/'); index = path.lastIndexOf('/'); if (index != -1) { if (index == path.length() - 1) { return ""; } path = path.substring(index + 1); } if (!includeExtension) { index = path.lastIndexOf('.'); if (index != -1) { path = path.substring(0, index); } } return path; } return null; } }