Java File Extension Name Extract getFileExtension(File file)

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

Description

get File Extension

License

Open Source License

Declaration

public static String getFileExtension(File file) 

Method Source Code

//package com.java2s;
/*//from   w  ww . j  a v  a2  s .c  om
 * Static String formatting and query routines.
 * Copyright (C) 2001,2002 Stephen Ostermiller <utils@Ostermiller.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * See COPYING.TXT for details.
 */

import java.io.File;

public class Main {
    public static String getFileExtension(File file) {
        return getFileExtension(file.getPath());
    }

    public static String getFileExtension(String pathName) {
        // getting the extension of a filename, (plain or including dirname)
        // This code is much faster than any regex technique.

        // filename without the extension
        String choppedFilename;

        // extension without the dot
        String ext;

        // where the last dot is. There may be more than one.
        int dotPlace = pathName.lastIndexOf('.');

        if (dotPlace >= 0) {
            // possibly empty
            choppedFilename = pathName.substring(0, dotPlace);

            // possibly empty
            ext = pathName.substring(dotPlace + 1);
        } else {
            // was no extension
            choppedFilename = pathName;
            ext = "";
        }
        return ext;
    }
}

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, boolean includeDelimiter)
  7. getFileExtension(File fx)
  8. getFileExtension(File path)
  9. getFileExtension(final String fileName)