Java File Name Extract extractFileName(String filePathName)

Here you can find the source of extractFileName(String filePathName)

Description

Extract file name (without path and suffix) from file name with path and suffix.

License

Open Source License

Parameter

Parameter Description
filePathName the file name with path and suffix

Return

the file name without path and suffix

Declaration


public static String extractFileName(String filePathName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * 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  .jav  a2  s  . com*/
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Extract file name (without path and suffix) from file name with path and
     * suffix.
     * <p>
     * For example:
     * <p>
     * <ul>
     * <li>"c:\home\abc.xml" => "abc"
     * <li>"c:\home\abc" => "abc"
     * <li>"/home/user/abc.xml" => "abc"
     * <li>"/home/user/abc" => "abc"
     * </ul>
     * 
     * @param filePathName
     *            the file name with path and suffix
     * @return the file name without path and suffix
     */

    public static String extractFileName(String filePathName) {
        if (filePathName == null)
            return null;

        int dotPos = filePathName.lastIndexOf('.');
        int slashPos = filePathName.lastIndexOf('\\');

        int backSlashPos = filePathName.lastIndexOf('/');
        slashPos = slashPos > backSlashPos ? slashPos : backSlashPos;

        if (dotPos > slashPos) {
            return filePathName.substring(slashPos > 0 ? slashPos + 1 : 0, dotPos);
        }

        return filePathName.substring(slashPos > 0 ? slashPos + 1 : 0);
    }
}

Related

  1. extractFileName(final String filePath)
  2. extractFileName(StackTraceElement aFrame)
  3. extractFileName(String file)
  4. extractFileName(String fileName)
  5. extractFileName(String filePathName)
  6. extractFileName(String fn)
  7. extractFileName(String fullPath)
  8. extractFileName(String name)