Here you can find the source of getFileNameWithoutExtension(File file)
Parameter | Description |
---|---|
file | the file path |
public static final String getFileNameWithoutExtension(File file)
//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 } }