Java File Extension Name Extract getFileExtension(String filePath)

Here you can find the source of getFileExtension(String filePath)

Description

Get the extension of a file.

License

Apache License

Parameter

Parameter Description
filePath the file from which to get the extension.

Return

the file extension, or null if it si not a file or does not have an extension.

Declaration

public static String getFileExtension(String filePath) 

Method Source Code

//package com.java2s;
/*/*from   w  w w.  j a v a2  s . c  o  m*/
 * JPPF.
 * Copyright (C) 2005-2010 JPPF Team.
 * http://www.jppf.org
 *
 * 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.
 */

import java.io.*;

public class Main {
    /**
     * Get the extension of a file.
     * @param filePath the file from which to get the extension.
     * @return the file extension, or null if it si not a file or does not have an extension.
     */
    public static String getFileExtension(String filePath) {
        return getFileExtension(new File(filePath));
    }

    /**
     * Get the extension of a file.
     * @param file the file from which to get the extension.
     * @return the file extension, or null if it si not a file or does not have an extension.
     */
    public static String getFileExtension(File file) {
        if ((file == null) || !file.exists() || !file.isFile())
            return null;
        String filePath = file.getPath();
        int idx = filePath.lastIndexOf(".");
        if (idx >= 0)
            return filePath.substring(idx + 1);
        return null;
    }
}

Related

  1. getFileExtension(final String fileName)
  2. getFileExtension(final String filenamePath)
  3. getFileExtension(String fileName)
  4. getFileExtension(String filename)
  5. getFileExtension(String filename, String extensionSeparator)
  6. getFileExtensionIgnoringGz(File f)