Java File Extension Name Extract getFileExtension(String fileName)

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

Description

Returns file's extention.

License

Open Source License

Parameter

Parameter Description
fileName The name of the file.

Declaration

public static String getFileExtension(String fileName) 

Method Source Code

//package com.java2s;

import java.io.*;

public class Main {
    /**//  w w w  .  ja v a2s. c  om
     * Returns file's extention.
     *
     * @param fileName The name of the file.
     */
    public static String getFileExtension(String fileName) {
        String ext = null;
        if (fileName.lastIndexOf(".") > 0 && fileName.lastIndexOf(".") < fileName.length()) {
            ext = fileName.substring(fileName.lastIndexOf(".") + 1);
        }
        return ext;
    }

    public static String getFileExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 && i < s.length() - 1) {
            ext = s.substring(i + 1).toLowerCase();
        }
        return ext;
    }
}

Related

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