Java File Extension Name Get getFileExtension(String fileName)

Here you can find the source of getFileExtension(String fileName)

Description

Get File extension (result will NOT include ".")

License

Open Source License

Parameter

Parameter Description
fileName a parameter

Return

String file extension value, or "" is no extension

Declaration

public static String getFileExtension(String fileName) 

Method Source Code

//package com.java2s;
/*//from w  ww  . j  av  a2s  .  c om
 * Copyright (c) 2012 Diamond Light Source Ltd.
 *
 * 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
 */

import java.io.File;

public class Main {
    /**
     * Get File extension (result will NOT include ".")
     * 
     * @param fileName
     * @return String file extension value, or "" is no extension
     */
    public static String getFileExtension(String fileName) {
        int posExt = fileName.lastIndexOf(".");
        // No File Extension
        return posExt == -1 ? "" : fileName.substring(posExt + 1);
    }

    /**
     * Get File extension (result will NOT include ".")
     * 
     * @param file
     * @return String file extension value, or "" is no extension
     */
    public static String getFileExtension(File file) {
        return getFileExtension(file.getName());
    }
}

Related

  1. getFileExtension(String file)
  2. getFileExtension(String filename)
  3. getFileExtension(String fileName)
  4. getFileExtension(String filename)
  5. getFileExtension(String fileName)
  6. getFileExtension(String fileName)
  7. getFileExtension(String filePath)
  8. getFileExtension(String filePath)
  9. getFileExtension(String filePath)