You can get extension from File name in Java.
Get extension by file name
You can think of the last .
in the file name as shown below and the string after it as the extension.
File file = new File("/home/js/test/myfile/file1.txt");
String fileName = file.getName();
String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
System.out.println("file name : " + fileName);
System.out.println("extension : " + ext);
result
file name : file1.txt
extension : txt
How to use Stream
You can also get the extension using Optional and Stream.
The following code gets the extension and Optional<String>
.
File file = new File("/home/js/test/myfile/file1.txt");
String fileName = file.getName();
Optional<String> ext = getExtensionByStringHandling(fileName);
ext.ifPresent(s -> System.out.println("extension : " + s));
public static Optional<String> getExtensionByStringHandling(String filename) {
return Optional.ofNullable(filename)
.filter(f -> f.contains("."))
.map(f -> f.substring(filename.lastIndexOf(".") + 1));
}
If the file is a folder, it has no extension. In this case, Optional will be null.
If there is an extension like file1.txt
, Optional will have a String.
Import extension using Commons-io library
You can easily get the extension by using FilenameUtils.getExtension()
of the Commons-io library.
To use Commons-io, in the case of a Gradle project, add the following to dependencies of build.gradle
.
dependencies {
compile group: 'commons-io', name: 'commons-io', version: '2.6'
...
}
For projects that use other build systems such as Maven, refer to mvnrepository.com and configure it.
The following example is the code to get the extension with FilenameUtils.
System.out.println();
File file = new File("/home/js/test/myfile/file1.txt");
String fileName = file.getName();
String ext = FilenameUtils.getExtension(fileName);
System.out.println("extension : " + ext);
If there is no extension such as a folder, an empty string (""
) is returned instead of null.
Reference
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)