How to Get the Filename Without the Extension in Java

How to get the filename without the extension in Java?

If you, like me, would rather use some library code where they probably have thought of all special cases, such as what happens if you pass in null or dots in the path but not in the filename, you can use the following:

import org.apache.commons.io.FilenameUtils;
String fileNameWithOutExt = FilenameUtils.removeExtension(fileNameWithExt);

How to get name of File object without its extension in Java?

If you want to implement this yourself, try this:

String name = file.getName();
int pos = name.lastIndexOf(".");
if (pos > 0) {
name = name.substring(0, pos);
}

(This variation doesn't leave you with an empty string for an input filename like ".txt". If you want the string to be empty in that case, change > 0 to >= 0.)


You could replace the if statement with an assignment using a conditional expression, if you thought it made your code more readable; see @Steven's answer for example. (I don't think it does ... but it is a matter of opinion.)


It is arguably a better idea to use an implementation that someone else has written and tested. Apache FilenameUtils is a good choice; see @slachnick's Answer, and also the linked Q&A.

Java - Getting file name without extension from a folder

This code will do the work of removing the extension and printing name of file:

      public static void main(String[] args) {
String path = "C:\\Users\\abc\\some";
File folder = new File(path);
File[] files = folder.listFiles();
String fileName;
int lastPeriodPos;
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
fileName = files[i].getName();
lastPeriodPos = fileName.lastIndexOf('.');
if (lastPeriodPos > 0)
fileName = fileName.substring(0, lastPeriodPos);
System.out.println("File name is " + fileName);
}
}
}

If you are ok with standard libraries then use Apache Common as it has ready-made method for that.

How do I get the file extension of a file in Java?

In this case, use FilenameUtils.getExtension from Apache Commons IO

Here is an example of how to use it (you may specify either full path or just file name):

import org.apache.commons.io.FilenameUtils;

// ...

String ext1 = FilenameUtils.getExtension("/path/to/file/foo.txt"); // returns "txt"
String ext2 = FilenameUtils.getExtension("bar.exe"); // returns "exe"

Maven dependency:

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>

Gradle Groovy DSL

implementation 'commons-io:commons-io:2.6'

Gradle Kotlin DSL

implementation("commons-io:commons-io:2.6")

Others https://search.maven.org/artifact/commons-io/commons-io/2.6/jar

How to get only filename without .mp3 and .mp4 extension in android?

Try this way to get filename without extension:-

if (fileName.indexOf(".") > 0)
fileName = fileName.substring(0, fileName.lastIndexOf("."));

How to trim a file extension from a String in JavaScript?

If you know the length of the extension, you can use x.slice(0, -4) (where 4 is the three characters of the extension and the dot).

If you don't know the length @John Hartsock regex would be the right approach.

If you'd rather not use regular expressions, you can try this (less performant):

filename.split('.').slice(0, -1).join('.')

Note that it will fail on files without extension.

Regex to get filename with or without extension from a path

The following regex will match desired parts:

^(?:.*\/)?([^\/]+?|)(?=(?:\.[^\/.]*)?$)

Explanation:

  • ^ Match start of the line
  • (?: Start of a non-capturing group

    • .*\/ Match up to last / character
  • )? End of the non-capturing (optional)
  • ([^\/]+?|) Capture anything but / ungreedily or nothing
  • (?= Start of a positive lookahead

    • (?:\.[^\/.]*)? Match an extension (optional)
    • $ Assert end of the line
  • ) End of the positive lookahead

but if you are dealing with a multi-line input string and need a bit faster regex try this one instead (with m flag on) :

^(?:[^\/\r\n]*\/)*([^\/\r\n]+?|)(?=(?:\.[^\/\r\n.]*)?$)

See live demo here

Filename would be captured in the first capturing group.



Related Topics



Leave a reply



Submit