21

What I did up until now is following:

String fileName = "file.date.txt";
String ext = fileName.substring(fileName.lastIndexOf('.') + 1);

System.out.printf("'%s'%n", ext); // prints: 'txt'

Is there a more convenient way in Java 8?

mike
  • 4,929
  • 4
  • 40
  • 80
  • 3
    The importance of a file extension is often overestimated. The `substring`/`lastIndexOf` solution is sufficient. – Holger Jun 18 '15 at 11:35
  • 2
    Java can not provide you with explicit means for getting the "file extension" of a path name because the concept of a file extension is not portable. Unix does not really have them (you can have a `.` near the end of a file leaf name, but the O/S kernel does not consider what follows to be significant). – Raedwald Jun 18 '15 at 11:56
  • 4
    Uhm. Extensions are a convenient and very common way of organizing files, used by a vast number of tools and applications, even if modern operating systems do not handle them explicitly. – Nicola Musatti Sep 19 '19 at 09:27

5 Answers5

17

No there is no more efficient/convenient way in JDK, but many libraries give you ready methods for this, like Guava: Files.getFileExtension(fileName) which wraps your code in single method (with additional validation).

Pshemo
  • 122,468
  • 25
  • 185
  • 269
13

Not Java8, but you can always use FilenameUtils.getExtension() from apache Commons library. :)

hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36
7

No, see the changelog of the JDK8 release

Binkan Salaryman
  • 3,008
  • 1
  • 17
  • 29
  • 2
    The changes made to java.io resp. java.nio are summarized here http://docs.oracle.com/javase/8/docs/technotes/guides/io/enhancements.html#jdk8 – mike Jun 18 '15 at 11:29
  • If this is an answer, I was expecting it to have some more context. :) as the one mentioned by @Pshemo much better and helpful. – Syed Siraj Uddin Jan 22 '19 at 13:22
2

Actually there is a new way of thinking about returning file extensions in Java 8.

Most of the "old" methods described in the other answers return an empty string if there is no extension, but while this avoids NullPointerExceptions, it makes it easy to forget that not all files have an extension. By returning an Optional, you can remind yourself and others about this fact, and you can also make a distinction between file names with an empty extension (dot at the end) and files without extension (no dot in the file name)

public static Optional<String> findExtension(String fileName) {
    int lastIndex = fileName.lastIndexOf('.');
    if (lastIndex == -1) {
        return Optional.empty();
    }
    return Optional.of(fileName.substring(lastIndex + 1));
}
lbalazscs
  • 17,474
  • 7
  • 42
  • 50
  • `Optional.of(fileName).filter(value -> value.contains(".")).map(value -> value.substring(value.lastIndexOf('.')));` – CedX Jul 21 '23 at 22:29
2

Use FilenameUtils.getExtension from Apache Commons IO

Example:

You can provide full path name or only the file name.

String myString1 = FilenameUtils.getExtension("helloworld.exe"); // returns "exe"
String myString2 = FilenameUtils.getExtension("/home/abc/yey.xls"); // returns "xls"

Hope this helps ..

Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
  • 11
    There is an almost three year old answer mentioning this. There is no need to write that again. – Tom Apr 06 '19 at 07:42