Java File Extension Name Extract getFileExtension(final String fileName)

Here you can find the source of getFileExtension(final String fileName)

Description

Identifies the file extension for a given file name and returns the extension part, or an empty string if the file has no extension.

License

Open Source License

Parameter

Parameter Description
fileName file name to parse

Return

never null

Declaration

public static String getFileExtension(final String fileName) 

Method Source Code


//package com.java2s;
/* Copyright (c) 2011-2012 Pushing Inertia
 * All rights reserved.  http://pushinginertia.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License./*from   w w  w  . j  a va2s .  c o  m*/
 */

import java.io.File;

public class Main {
    /**
     * Identifies the file extension for a given file name and returns the extension part, or an empty string if the
     * file has no extension.
     * @param fileName file name to parse
     * @return never null
     */
    public static String getFileExtension(final String fileName) {
        return getFileExtension(File.separatorChar, fileName);
    }

    /**
     * Identifies the file extension for a given file name and returns the extension part, or an empty string if the
     * file has no extension.
     * @param dirSeparator character used to separate directories on the running OS
     * @param fileName file name to parse
     * @return never null
     */
    public static String getFileExtension(final char dirSeparator, final String fileName) {
        final int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex < 0) {
            return "";
        }

        final int dirSeparatorIndex = fileName.lastIndexOf(dirSeparator);
        if (dotIndex < dirSeparatorIndex) {
            return "";
        }

        return fileName.substring(dotIndex + 1);
    }
}

Related

  1. getFileExtension(File file)
  2. getFileExtension(File file)
  3. getFileExtension(File file, boolean includeDelimiter)
  4. getFileExtension(File fx)
  5. getFileExtension(File path)
  6. getFileExtension(final String filenamePath)
  7. getFileExtension(String fileName)
  8. getFileExtension(String filename)
  9. getFileExtension(String filename, String extensionSeparator)