Java File Name Get getFileNameWithoutExtension(File file)

Here you can find the source of getFileNameWithoutExtension(File file)

Description

returns a file's name excluding the extension (by default file name extension separator is a period '.')

License

Open Source License

Parameter

Parameter Description
file the file path

Return

the file name as a string

Declaration

public static final String getFileNameWithoutExtension(File file) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {
    /** returns a file's name excluding the extension (by default file name extension separator is a period '.')
     * @param file the file path//from  w ww .ja  v  a 2  s . c  o m
     * @return the file name as a string
     */
    public static final String getFileNameWithoutExtension(File file) {
        return getFileExtension(file.getName());
    }

    /** returns a file's name at the end of a file path excluding the extension (by default file name extension separator is a period '.')
     * @param filepath the file path
     * @return the file name as a string
     */
    public static final String getFileNameWithoutExtension(String filepath) {
        // Get the file type by getting the sub string starting at the last index of '.'
        int dotIdx = -1;
        int separatorIdx = -1;
        int fileLen = filepath.length();
        for (int i = fileLen - 1; i > -1; i--) {
            char ch = filepath.charAt(i);
            // last '.' dot index
            if (dotIdx < 0 && ch == '.') {
                dotIdx = i;
            }
            // break on last separator
            if (ch == '/' || ch == '\\') {
                separatorIdx = i;
                break;
            }
        }

        return filepath.substring(separatorIdx > -1 ? separatorIdx + 1 : 0, dotIdx > -1 ? dotIdx : fileLen);
    }

    /** returns a file's extension/type, excluding the extension separator (by default a period '.')
     * @param file the file name with extension
     * @return the file extension as a string or null if the file extension cannot be identified
     */
    public static final String getFileExtension(File file) {
        return getFileExtension(file.getName());
    }

    /** returns a file's extension/type, excluding the extension separator (by default a period '.')
     * @param filepath the file name with extension
     * @return the file extension as a string or empty string if the file extension cannot be identified
     */
    public static final String getFileExtension(String filepath) {
        // Get the file type by getting the sub string starting at the last index of '.'
        int dotIdx = -1;
        int separatorIdx = -1;
        int fileLen = filepath.length();
        for (int i = fileLen - 1; i > -1; i--) {
            char ch = filepath.charAt(i);
            // last '.' dot index
            if (dotIdx < 0 && ch == '.') {
                dotIdx = i;
            }
            // break on last separator
            if (ch == '/' || ch == '\\') {
                separatorIdx = i;
                break;
            }
        }

        return fileLen <= dotIdx + 1 || dotIdx < separatorIdx ? "" : filepath.substring(dotIdx + 1); // Return the ending substring
    }
}

Related

  1. getFileNameWithoutExt(String originPath)
  2. getFilenameWithoutExtension(File f)
  3. getFileNameWithoutExtension(File file)
  4. getFileNameWithoutExtension(File file)
  5. getFileNameWithoutExtension(File file)
  6. getFileNameWithoutExtension(File file)
  7. getFileNameWithoutExtension(File file)
  8. getFileNameWithoutExtension(File file)
  9. getFileNameWithoutExtension(File fx)