Java File Extension Name Extract getFileExtension(File file)

Here you can find the source of getFileExtension(File file)

Description

Gets the file extension as a string.

License

Open Source License

Parameter

Parameter Description
file The file in which the extension must be extracted.

Return

The extension of a given file as a string.

Declaration

public static String getFileExtension(File file) 

Method Source Code


//package com.java2s;
import java.io.File;

public class Main {
    /**/*from w  ww  .jav a  2 s .co m*/
     * Gets the file extension as a string. For example, given the file "test.xml",
     * the result of this method consists of "xml". Notice that if no extension is
     * found in the file, the string returned is empty.
     * 
     * @param file The file in which the extension must be extracted.
     * @return The extension of a given file as a string.
     */
    public static String getFileExtension(File file) {
        String fileName = file.getName();

        return (fileName.lastIndexOf(".") == -1) ? ""
                : fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
    }

    /**
     * Gets the file extension as a string. For example, given the file "test.xml",
     * the result of this method consists of "xml". Notice that if no extension is
     * found in the file, the string returned is empty.
     * 
     * @param filefileName the name of the file.
     * @return The extension of a given file name as a string.
     */
    //INSPECT - Lais New Method
    public static String getFileExtension(String fileName) {
        return (fileName.lastIndexOf(".") == -1) ? ""
                : fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
    }
}

Related

  1. getFileExtension(File file)
  2. getFileExtension(File file)
  3. getFileExtension(File file)
  4. getFileExtension(File file)
  5. getFileExtension(File file)
  6. getFileExtension(File file)
  7. getFileExtension(File file, boolean includeDelimiter)
  8. getFileExtension(File fx)
  9. getFileExtension(File path)