Here you can find the source of getFileExtension(File file)
Parameter | Description |
---|---|
file | a parameter |
public static String getFileExtension(File file)
//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) }; } } }