Java File Extension Name Get getFileExtension(File f)

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

Description

Return the file extension (the last part of the name after the last '.').

License

Open Source License

Parameter

Parameter Description
f The file

Return

The file extension

Declaration

public static String getFileExtension(File f) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Firestar Software, Inc.
 * 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  ww.j a  v a 2  s. co m
 *     Firestar Software, Inc. - initial API and implementation
 *
 * Author:
 *     Gabriel Oancea
 *
 *******************************************************************************/

import java.io.*;

public class Main {
    /**
     * Return the file extension (the last part of the name after the last '.').
     * 
     * @param f The file
     * @return The file extension
     */
    public static String getFileExtension(File f) {
        if (f == null)
            return null;
        String fn = f.getName();
        if (fn.length() <= 0 || fn.startsWith(".")) // ., .., and any .xxx
            return fn;
        int p = fn.lastIndexOf('.');
        if (p < 0)
            return "";
        if (p == 0)
            return fn.substring(1);
        if (p == fn.length() - 1)
            return "";
        return fn.substring(p + 1);
    }
}

Related

  1. getFileExtension(File _file)
  2. getFileExtension(File f)
  3. getFileExtension(File f)
  4. getFileExtension(File f)
  5. getFileExtension(File f)
  6. getFileExtension(File f)
  7. getFileExtension(File f)
  8. getFileExtension(File f)
  9. getFileExtension(File f)