Java Path File Extention nio getFilenameWithoutExtension(Path file)

Here you can find the source of getFilenameWithoutExtension(Path file)

Description

Gets the filename without the extension

License

Open Source License

Parameter

Parameter Description
file Path to the file

Exception

Parameter Description
Exception File not have extension

Return

Filename without extension

Declaration

public static String getFilenameWithoutExtension(Path file) 

Method Source Code

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

import java.nio.file.Path;

public class Main {
    /**//from   ww w. j a v a 2s  . co m
     * Gets the filename without the extension
     * 
     * @param file         Path to the file
     * @return            Filename without extension
     * @throws Exception   File not have extension
     */
    public static String getFilenameWithoutExtension(Path file) {
        return getFilenameWithoutExtension(file.getFileName().toString());
    }

    /**
     * Gets the filename without the extension
     * 
     * @param fullFilename   Full filename
     * @return            Filename without extension
     * @throws Exception   File not have extension
     */
    public static String getFilenameWithoutExtension(String fullFilename) {
        String[] tokens = fullFilename.split("\\.");

        // if the file does not have an extension
        if (tokens.length <= 1) {
            return fullFilename;
        }

        String filename = new String();
        for (int i = 0; i < tokens.length - 1; i++) {
            filename += tokens[i];
        }

        return filename;
    }
}

Related

  1. getFileExtension(Path path)
  2. getFileExtension(Path path)
  3. getFileExtension(Path path)
  4. getFileExtension(Path path)
  5. getFileNameNoExtensionFromPath(String modulePath)
  6. getFileNameWithoutExtension(Path path)
  7. getFileNameWithoutExtension(Path path)
  8. getFileNameWithoutExtension(String filePath)
  9. getFilesList(Path directory, Set extensions)