Java examples for java.io:File Extension Name
Returns the filename extension without the dot using regex.
/*/*from w ww .j ava2 s .c om*/ * JCamStream, simple Java application for video surveillance from webcams. * Copyright (C) 2011 Papa Issa DIAKHATE (paissad). * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see <http://www.gnu.org/licenses/>. */ //package com.java2s; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] argv) throws Exception { String filename = "java2s.com"; System.out.println(getFilenameExtension(filename)); } /** * Returns the filename extension without the dot. * * @param filename * @return The extension without the dot, or <code>null</code> if the * filename is null, or an empty String if the file has no * extension. */ public static String getFilenameExtension(final String filename) { if (filename == null) return null; Pattern pattern = Pattern.compile("\\.[^\\.]*$"); Matcher matcher = pattern.matcher(filename); int count = matcher.groupCount(); return (matcher.find()) ? matcher.group(count) : ""; } }