Java File Extension Name Get getFileExtension(String filePath)

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

Description

Get file extension

License

Open Source License

Parameter

Parameter Description
filePath The file path

Return

File extension

Declaration

public static String getFileExtension(String filePath) 

Method Source Code

//package com.java2s;
/* Copyright 2012 Yaqiang Wang,
* yaqiang.wang@gmail.com// w w w  . j av  a  2  s  .c o m
* 
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
* 
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
* General Public License for more details.
*/

import java.io.File;

public class Main {
    /**
     * Get file extension
     *
     * @param filePath The file path
     * @return File extension
     */
    public static String getFileExtension(String filePath) {
        String extension = "";
        String fn = new File(filePath).getName();
        if (fn.contains(".")) {
            String ext = filePath.substring(filePath.lastIndexOf(".") + 1).toLowerCase().trim();
            try {
                extension = ext;
            } catch (IllegalArgumentException e) {
            }
        }

        return extension;
    }
}

Related

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