Java tutorial
//package com.java2s; /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import java.io.File; public class Main { public static String getFileExtension(File file) { return getFileExtension(file.getPath()); } public static String getFileExtension(String path) { // 'archive.tar.gz' -> 'gz' // '/path/to.a/file' -> '' // '/root/case/g.txt' -> 'txt' // '/root/case/g.txt.gg' -> 'gg' // '/root/case/g.txt.gg/' -> '' // '.htaccess' -> 'htaccess' // '/.htaccess' -> 'htaccess' // '/s/.htaccess' -> 'htaccess' String extension = null; int i = path.lastIndexOf('.'); int p = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\')); if (i > p) { extension = path.substring(i + 1); } return extension; } }