Java File Name Get getFileName(String f)

Here you can find the source of getFileName(String f)

Description

This method returns the file name of a given file-path which is passed as parameter.

License

Open Source License

Parameter

Parameter Description
f the filepath of the file which file name should be retrieved

Return

the name of the given file, excluding extension, or null if an error occured.

Declaration

public static String getFileName(String f) 

Method Source Code

//package com.java2s;
/*//  w ww  . j  ava 2s .  c o  m
 * Zettelkasten - nach Luhmann
 ** Copyright (C) 2001-2014 by Daniel L?decke (http://www.danielluedecke.de)
 * 
 * Homepage: http://zettelkasten.danielluedecke.de
 * 
 * 
 * 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/>.
 * 
 * 
 * Dieses Programm ist freie Software. Sie k?nnen es unter den Bedingungen der GNU
 * General Public License, wie von der Free Software Foundation ver?ffentlicht, weitergeben
 * und/oder modifizieren, entweder gem?? Version 3 der Lizenz oder (wenn Sie m?chten)
 * jeder sp?teren Version.
 * 
 * Die Ver?ffentlichung dieses Programms erfolgt in der Hoffnung, da? es Ihnen von Nutzen sein 
 * wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die implizite Garantie der MARKTREIFE oder 
 * der VERWENDBARKEIT F?R EINEN BESTIMMTEN ZWECK. Details finden Sie in der 
 * GNU General Public License.
 * 
 * Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm 
 * erhalten haben. Falls nicht, siehe <http://www.gnu.org/licenses/>.
 */

import java.io.File;

public class Main {
    /**
     * This method returns the file name of a given file-path which is passed as parameter.
     *
     * @param f the filepath of the file which file name should be retrieved
     * @return the name of the given file, excluding extension, or {@code null} if an error occured.
     */
    public static String getFileName(String f) {
        String fn = null;
        int i = f.lastIndexOf(String.valueOf(File.separatorChar));
        if (i != -1) {
            int j = f.lastIndexOf(".");
            if (j != -1) {
                try {
                    fn = f.substring(i + 1, j);
                } catch (IndexOutOfBoundsException ex) {
                    return null;
                }
            }
        }
        return fn;
    }

    /**
     * This method returns the file name of a given file-path which is passed as parameter.
     *
     * @param f the filepath of the file which file name should be retrieved
     * @return the name of the given file, excluding extension, or {@code null} if an error occured.
     */
    public static String getFileName(File f) {
        if (f != null) {
            String fname = f.getName();
            int extpos = fname.lastIndexOf(".");
            if (extpos != -1) {
                try {
                    return fname.substring(0, extpos);
                } catch (IndexOutOfBoundsException ex) {
                    return null;
                }
            }
        }
        return null;
    }
}

Related

  1. getFileName(final String subdirectory)
  2. getFilename(Locale locale, String fileName, String fileType)
  3. getFileName(String absolutePath)
  4. getFileName(String completePath)
  5. getFileName(String dir, String name)
  6. getFileName(String fileName)
  7. getFileName(String filePath)
  8. getFileName(String filePath)
  9. getFileName(String filepath)