1. Overview
1.概述
In this quick tutorial, we’ll show how to obtain the file extension programmatically in Java. We’ll focus on three major approaches to the problem.
在这个快速教程中,我们将展示如何在Java中以编程方式获取文件扩展名。我们将重点讨论解决该问题的三种主要方法。
In our implementations, the characters after the final ‘.’ will be returned.
在我们的实现中,最后的’.’之后的字符。之后的字符将被返回。
Therefore, as a quick example, if our file name is jarvis.txt then it will return the String “txt” as the file’s extension.
因此,作为一个简单的例子,如果我们的文件名是jarvis.txt,那么它将返回String “txt” 作为文件的扩展名。
2. Getting the File Extension
2.获取文件扩展名
For each approach, we’ll learn how to implement it and follow up with what happens in two special cases:
对于每一种方法,我们将学习如何实现它,并跟进在两个特殊情况下发生的情况。
- when a filename has no extensions, such as a makefile file
- and if a filename consists of the extension only, such as .gitignore or .DS_Store.
2.1. Simple String Handling Approach
2.1.简单的字符串处理方法
With this approach, we’ll use a simple String handling approach to finding the extension:
通过这种方法,我们将使用一个简单的String处理方法来寻找扩展名。
public Optional<String> getExtensionByStringHandling(String filename) {
return Optional.ofNullable(filename)
.filter(f -> f.contains("."))
.map(f -> f.substring(filename.lastIndexOf(".") + 1));
}
This method will check for the dot ‘.’ occurrence in the given filename.
该方法将检查在给定的文件名中是否有点’.’出现。
If it exists, then it will find the last position of the dot ‘.’ and return the characters after that, the characters after the last dot ‘.’ known as the file extension.
如果它存在,那么它将找到最后一个点’.’的位置并返回之后的字符,最后一个点’.’之后的字符被称为文件扩展名。
Special Cases:
特殊案例。
- No extension – this method will return an empty String
- Only extension – this method will return the String after the dot, e.g. “gitignore”
2.2. FilenameUtils.getExtension from Apache Commons IO
2.2.FilenameUtils.getExtension 来自 Apache Commons IO
In the second approach, we’ll find the extension using a utility class provided by Apache Commons IO library:
在第二种方法中,我们将使用Apache Commons IO库提供的一个实用类来寻找扩展。
public String getExtensionByApacheCommonLib(String filename) {
return FilenameUtils.getExtension(filename);
}
Here, instead of the file name, we can also specify the full path to a file e.g. “C:/baeldung/com/demo.java“.
在这里,我们也可以不指定文件名,而是指定文件的完整路径,例如“C:/baeldung/com/demo.java“。
The method getExtension(String) will check whether the given filename is empty or not.
方法getExtension(String)将检查给出的filename是否为空。
If filename is empty or null, getExtension(String filename) will return the instance it was given. Otherwise, it returns extension of the filename.
如果filename是空的或空的,getExtension(String filename)将返回它被赋予的实例。否则,它将返回文件名的扩展名。。
To do this it uses the method indexOfExtension(String) which, in turn, uses lastIndexof(char) to find the last occurrence of the ‘.’. These methods are both provided by FilenameUtils.
为了做到这一点,它使用了indexOfExtension(String)方法,而该方法又使用了lastIndexof(char)来查找最后出现的”。这些方法都是由FilenameUtils提供的。
This method also checks that there’s no directory separator after the last dot by using another method indexOfLastSeparator(String), which will handle a file in either Unix or Windows format.
这个方法还通过使用另一个方法indexOfLastSeparator(String),来检查最后一个点之后是否有目录分隔符,这个方法将处理Unix或Windows格式的文件。
Special Cases:
特殊案例。
- No extension – this method will return an empty string.
- Only extension – this method will return the String after the dot, e.g. “gitignore”
2.3. Using the Guava Library
2.3.使用Guava库
In this last approach, we’ll use Guava library to find the extension.
在这最后一种方法中,我们将使用Guava库来寻找扩展。
To add a Guava library, we can add the following dependency to our pom.xml:
要添加一个Guava库,我们可以在我们的pom.xml中添加以下依赖关系:。
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
</dependency>
For latest dependency, we can check Maven Central.
关于最新的依赖性,我们可以查看Maven Central。
After adding the library we can simply use its getFileExtension method:
添加库后,我们可以简单地使用其getFileExtension方法。
public String getExtensionByGuava(String filename) {
return Files.getFileExtension(filename);
}
The method getFileExtension(String) will first check whether the given filename is empty.
方法getFileExtension(String)将首先检查给出的filename是否为空。
If the filename isn’t empty, then it will create a File instance by converting the given filename into an abstract pathname and call File’s getName() method over it, which will return the name of the file denoted by this abstract pathname, or the empty string if the given filename is empty.
如果filename不是空的,那么它将创建一个File实例,将给定的filename转换为一个抽象路径名,并对其调用File的getName()方法,该方法将返回由该抽象路径名表示的文件名,如果给定的filename是空的,则为空字符串。
Based upon this return value it fetches the index of the last occurrence of the ‘.’ by using String class inbuilt method lastIndexOf(char).
基于这个返回值,它通过使用String类内置的方法lastIndexOf(char)来获取’.’最后出现的索引。
Special Cases:
特殊案例。
- No extension – this method will return an empty String
- Only extension – this method will return the String after the dot, e.g. “gitignore”
3. Conclusion
3.总结
When picking between Apache Commons and Guava, while both libraries have some common features and they also have functionality that’s absent from their alternative.
当在ApacheCommons和Guava之间进行选择时,虽然这两个库都有一些共同的特性,但它们也有一些功能是它们的替代品所没有的。
This means that if certain functionality is required, pick the one that has it. Otherwise, if more custom scenarios are required, pick the one that does the most of what you need and feel free to wrap it with your own implementation to get the desired result.
这意味着,如果需要某些功能,就选有这些功能的那一个。否则,如果需要更多的自定义方案,就选一个能完成你所需要的大部分功能的方案,并随时用你自己的实现来包装它,以获得所需的结果。
Also, check out all the examples in this article on Github.
此外,请查看本文Github上的所有例子。