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