Java File Extension Name Get getFileExtension(String fileName)

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

Description

Returns the extension of the given file without leading dot symbol.

License

Open Source License

Parameter

Parameter Description
fileName the file name (can also be file path) to extract extension from

Return

the extension of the given file or an empty string if the file has no extension

Declaration

public static String getFileExtension(String fileName) 

Method Source Code


//package com.java2s;

import java.io.File;

public class Main {
    /**//w  w  w.j  ava 2s .  c om
     * Returns the extension of the given file without leading dot symbol.
     * 
     * @param fileName
     *            the file name (can also be file path) to extract extension from
     * 
     * @return the extension of the given file or an empty string if the file has no extension
     */
    public static String getFileExtension(String fileName) {
        fileName = new File(fileName).getName();
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex == -1) {
            return "";
        }
        return fileName.substring(dotIndex + 1);
    }
}

Related

  1. getFileExtension(String filename)
  2. getFileExtension(String fileName)
  3. getFileExtension(String filename)
  4. getFileExtension(String fileName)
  5. getFileExtension(String fileName)
  6. getFileExtension(String filePath)
  7. getFileExtension(String filePath)
  8. getFileExtension(String filePath)
  9. GetFileExtension(String fname)