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

Parameter

Parameter Description
file a parameter

Return

the file-extension of the given file or null when file has no file-extension. Example "foo.png" will return "png".

Declaration

public static String getFileExtension(File file) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2015 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   w w w . j a va2  s  .co m*/
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.io.File;

public class Main {
    /**
     * @param file
     * @return the file-extension of the given file or null when file has no file-extension. Example "foo.png" will return
     *         "png".
     */
    public static String getFileExtension(File file) {
        if (file == null) {
            return null;
        }
        return getFileExtension(file.getName());
    }

    /**
     * @param fileName
     * @return the file-extension of the given file or null when file has no file-extension. Example "foo.png" will return
     *         "png".
     */
    public static String getFileExtension(String fileName) {
        String[] parts = getFilenameParts(fileName);
        if (parts == null) {
            return null;
        }
        return parts[1];
    }

    /**
     * @param file
     * @return an array with two elements, [0] contains the file-name without extension [1] contains the file-extension
     */
    public static String[] getFilenameParts(File file) {
        if (file == null) {
            return null;
        }
        return getFilenameParts(file.getName());

    }

    /**
     * @param fileName
     * @return an array with two elements, [0] contains the file-name without extension [1] contains the file-extension
     */
    public static String[] getFilenameParts(String fileName) {
        if (fileName == null) {
            return null;
        }
        int index = fileName.lastIndexOf('.');
        if (index < 0) {
            return new String[] { fileName, null };
        } else {
            return new String[] { fileName.substring(0, index), fileName.substring(index + 1) };
        }
    }
}

Related

  1. extractFileExtensionFromPath(String path)
  2. getFileExtension(File f)
  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)