Java File Name Get getFilename(final String pPath)

Here you can find the source of getFilename(final String pPath)

Description

Extracts the filename of a complete filename path.

License

Open Source License

Parameter

Parameter Description
pPath The full filename path.

Return

the extracted filename.

Declaration

public static String getFilename(final String pPath) 

Method Source Code


//package com.java2s;
import java.io.*;

public class Main {
    /**/*from ww w .j  a  v a  2 s  .co m*/
     * Extracts the filename of a complete filename path.
     *
     * @param pPath The full filename path.
     * @return the extracted filename.
     * @see File#getName
     * @see #getDirectoryname
     */
    public static String getFilename(final String pPath) {
        return getFilename(pPath, File.separatorChar);
    }

    /**
     * Extracts the filename of a complete filename path.
     *
     * @param pPath      The full filename path.
     * @param pSeparator The file separator.
     * @return the extracted filename.
     * @see File#getName
     * @see #getDirectoryname
     */
    public static String getFilename(final String pPath, final char pSeparator) {
        int index = pPath.lastIndexOf(pSeparator);

        if (index < 0) {
            return pPath; // Assume only filename
        }

        return pPath.substring(index + 1);
    }
}

Related

  1. getFileName(final File file)
  2. getFilename(final File file)
  3. getFileName(final File file)
  4. getFilename(final String filenamePath)
  5. getFileName(final String p)
  6. getFileName(final String subdirectory)
  7. getFilename(Locale locale, String fileName, String fileType)
  8. getFileName(String absolutePath)
  9. getFileName(String completePath)