Java File Extension Name Get getFileExtension(File _file)

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

Description

Extracts the file extension (part behind last dot of a filename).

License

Open Source License

Parameter

Parameter Description
_file file

Return

extension, empty string if no dot was found in filename or null if given String was null

Declaration

public static String getFileExtension(File _file) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {
    /**//  w ww  . jav a 2  s.c  o m
     * Extracts the file extension (part behind last dot of a filename).
     * Only returns the extension, without the leading dot.
     *
     * @param _fileName filename
     * @return extension, empty string if no dot was found in filename or null if given String was null
     */
    public static String getFileExtension(String _fileName) {
        if (_fileName == null) {
            return null;
        }
        int lastDot = _fileName.lastIndexOf('.');
        if (lastDot == -1) {
            return "";
        }
        return _fileName.substring(lastDot + 1);
    }

    /**
     * Extracts the file extension (part behind last dot of a filename).
     * Only returns the extension, without the leading dot.
     *
     * @param _file file
     * @return extension, empty string if no dot was found in filename or null if given String was null
     */
    public static String getFileExtension(File _file) {
        return getFileExtension(_file.getAbsolutePath());
    }
}

Related

  1. fileExt(String name)
  2. fileExt(String url)
  3. fileExtension(CharSequence path)
  4. fileExtension(String filename)
  5. fileExtension(String path)
  6. getFileExtension(File f)
  7. getFileExtension(File f)
  8. getFileExtension(File f)
  9. getFileExtension(File f)