Java File Name Get getFileName(File path)

Here you can find the source of getFileName(File path)

Description

Returns the file name (without extension) given a absolute path.

License

Apache License

Declaration

public static String getFileName(File path) 

Method Source Code

//package com.java2s;
/* Copyright (C) 2003-2016 Patrick G. Durand
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.//w  w w.j  ava 2 s.c  om
 */

import java.io.File;

public class Main {
    /**
     * Returns the file name (without extension) given a absolute path. If path
     * denotes a directory, then the last element of the absolute path is
     * returned. It path denotes a file, then the last element of the absolute
     * path is returned but without its extension (if any). The file extension is
     * recognized by looking for the last occurrence of a dot. Note that the
     * method may return null if no file name can be found.
     */
    public static String getFileName(File path) {
        String fName;
        int idx;

        fName = path.getName();
        if (fName.length() == 0)
            return null;
        idx = fName.lastIndexOf('.');
        if (idx < 0)
            return fName;
        return fName.substring(0, idx);
    }
}

Related

  1. getFilename(File file)
  2. getFileName(File file)
  3. getFileName(File file)
  4. getFilename(File file)
  5. getFilename(File file, boolean withExtension)
  6. getFileName(final File file)
  7. getFilename(final File file)
  8. getFileName(final File file)
  9. getFilename(final String filenamePath)