Java File Extension Name Get getFileExtension(File file)

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

Description

Returns the file extension of the specified file.

License

Open Source License

Parameter

Parameter Description
file The file to get the extension from

Return

The file extension or empty string if no extension was found

Declaration


public static String getFileExtension(File file) 

Method Source Code


//package com.java2s;
/*/*w  w w. j a  va2 s .co m*/
 * Copyright (C) 2006-2012 Klaus Reimer <k@ailis.de>
 * 
 * This program 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 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 Lesser General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program; if not, write to the Free Software Foundation, 
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.io.File;

public class Main {
    /**
     * Returns the file extension of the specified file. If the file has no
     * extension then an empty string is returned.
     * 
     * @param file
     *            The file to get the extension from
     * @return The file extension or empty string if no extension was found
     */

    public static String getFileExtension(File file) {
        int pos;
        String filename;

        filename = file.getName();
        pos = filename.lastIndexOf('.');
        if (pos >= 0) {
            return filename.substring(pos + 1);
        } else {
            return "";
        }

    }

    /**
     * Returns the file extension of the specified filename. If the filename has
     * no extension then an empty string is returned.
     * 
     * @param filename
     *            The filename to get the extension from
     * @return The file extension or empty string if no extension was found
     */

    public static String getFileExtension(String filename) {
        return getFileExtension(new File(filename));
    }
}

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)
  8. getFileExtension(File file)
  9. getFileExtension(File file)