Java File Name Extract extractFileName(String path)

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

Description

Extracts filename portion out of supplied pathname (leaves last segment only)

License

Open Source License

Parameter

Parameter Description
path absolute or relative path with forward slashes as delimiters

Return

the result filename

Declaration

static public String extractFileName(String path) 

Method Source Code

//package com.java2s;
/*******************************************************************************
* Copyright (c) 2015 ARM Ltd. and others
* 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   www . j a va  2  s .  c o  m*/
* ARM Ltd and ARM Germany GmbH - Initial API and implementation
*******************************************************************************/

public class Main {
    /**
     * Extracts filename portion out of supplied pathname (leaves last segment only)  
     * @param path absolute or relative path with forward slashes as delimiters
     * @return the result filename 
     */
    static public String extractFileName(String path) {
        if (path == null || path.isEmpty())
            return path;

        int pos = path.lastIndexOf('/');
        if (pos < 0)
            pos = path.lastIndexOf('\\');
        if (pos >= 0)
            return path.substring(pos + 1);
        return path;
    }
}

Related

  1. extractFileName(String fullPath)
  2. extractFileName(String name)
  3. extractFilename(String path)
  4. extractFileName(String path)
  5. extractFileName(String path)
  6. extractFilename(String path)
  7. extractFileName(String url)
  8. extractFileNameFromBAMLocation(String location)
  9. extractFileNameFromContentDisposition(String contentDisposition)