Java File Extension Name Get getFileExtension(final String file)

Here you can find the source of getFileExtension(final String file)

Description

Returns the file extension of a file

License

Open Source License

Parameter

Parameter Description
file to get extension of

Return

file extension

Declaration

public static String getFileExtension(final String file) 

Method Source Code


//package com.java2s;
/*/*  w  ww .ja v a2s  .  co  m*/
 * This file is part of the "STARDUST" project.
 *
 * (c) Fabian Keller <hello@fabian-keller.de>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

import java.io.File;

public class Main {
    /**
     * Returns the file extension of a file
     *
     * @param file to get extension of
     * @return file extension
     * @see http://stackoverflow.com/a/21974043/1262901
     */
    public static String getFileExtension(final File file) {
        return getFileExtension(file.getName());
    }

    /**
     * Returns the file extension of a file
     *
     * @param file to get extension of
     * @return file extension
     * @see http://stackoverflow.com/a/21974043/1262901
     */
    public static String getFileExtension(final String file) {
        final int lastIndexOf = file.lastIndexOf('.');
        if (lastIndexOf == -1) {
            return ""; // empty extension
        }
        return file.substring(lastIndexOf + 1);
    }
}

Related

  1. getFileExtension(File file, boolean includeDot)
  2. getFileExtension(File file, boolean withDot)
  3. getFileExtension(final File aFile)
  4. getFileExtension(final File file)
  5. getFileExtension(final File file)
  6. getFileExtension(final String fullName)
  7. getFileExtension(String file)
  8. getFileExtension(String fileName)
  9. getFileExtension(String fileName)