Java File Extension Name Extract getFileExtension(final String filenamePath)

Here you can find the source of getFileExtension(final String filenamePath)

Description

Retourniert Dateiendung (mit Punkt).

License

Open Source License

Declaration

public static String getFileExtension(final String filenamePath) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {
    public static String DIRECTORY_SEPARATOR = File.separator;

    /**/*from  www.ja va2  s.c o m*/
     * Retourniert Dateiendung (mit Punkt). Falls keine Endung gefunden wird, wird ein leerer String
     * retourniert.
     */
    public static String getFileExtension(final String filenamePath) {
        String filename = getFilename(filenamePath);

        if (filename.lastIndexOf(".") > 0) { //$NON-NLS-1$
            return filename.substring(filename.lastIndexOf("."), filename.length()); //$NON-NLS-1$

        }

        return ""; //$NON-NLS-1$
    }

    /**
     * Retourniert Dateinamen ohne Pfad als String
     */
    public static String getFilename(final String filenamePath) {
        String correctFilenamePath = getCorrectSeparators(filenamePath);

        if (correctFilenamePath.indexOf(DIRECTORY_SEPARATOR) < 0) {
            return filenamePath;
        }
        return correctFilenamePath.substring(correctFilenamePath.lastIndexOf(DIRECTORY_SEPARATOR) + 1,
                correctFilenamePath.length());
    }

    private static String getCorrectSeparators(final String pathOrFilename) {
        return pathOrFilename.replace("\\", DIRECTORY_SEPARATOR).replace("//", //$NON-NLS-1$ //$NON-NLS-2$
                DIRECTORY_SEPARATOR).replace("/", DIRECTORY_SEPARATOR); //$NON-NLS-1$
    }
}

Related

  1. getFileExtension(File file)
  2. getFileExtension(File file, boolean includeDelimiter)
  3. getFileExtension(File fx)
  4. getFileExtension(File path)
  5. getFileExtension(final String fileName)
  6. getFileExtension(String fileName)
  7. getFileExtension(String filename)
  8. getFileExtension(String filename, String extensionSeparator)
  9. getFileExtension(String filePath)