Write a program to read a file name as a string and find out the file extension and return it as output. For example, the file sun.gif has the extension gif.
Input and Output Format
- Input consists of a string that corresponds to a file name.
- The output consists of a string(extension of the input string (filename)).
Refer sample output for formatting specifications
Sample Input 1:
sun.gif
Sample Output 1:
Gif
Program to Find File Extension in Java
Following are the steps to find the extension of the file:
- Input file name from the user as a string.
- Pass the name to extensionString() method.
- Inside the method, create an object of string tokenizer for the specified string and separate the token with ‘.’ (dot) as a delimiter.
- Now, get the token before the ‘.’ (dot) by calling nextToken().
- At last, return it to the user.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
System.out.println(extensionString(s1));
}
public static String extensionString(String s1) {
StringTokenizer t = new StringTokenizer(s1, ".");
t.nextToken();
String s2 = t.nextToken();
return s2;
}
}
Output
Get File Extension using Library
Here, we will use the external library to get the extension of the file. We have a Utility class inside Apache Common IO. Use the following steps to use it:
- You need to create a Maven project using any IDE (We are using Eclipse IDE)
- Next, add the following dependency into the pom.xml file
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
- At last, use the utility class to fetch the extension name of the file and call the getExtension() method.
Program to get extension file in Java using Apache Common IO
package com.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.io.FilenameUtils;
public class Main {
public static void main(String args[]) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String s1 = bf.readLine();
System.out.print(FilenameUtils.getExtension(s1));
}
}
Output
Extract File Extension using Guava Library
Here, we will use another external library to get the extension of the file. We have a Utility class inside Guava Library. Use the following steps to use it:
- You need to create a Maven project using any IDE (We are using Eclipse IDE)
- Next, add the following dependency into the pom.xml file
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
</dependency>
- At last, use the utility class to fetch the extension name of the file and call the getFileExtension() method.
Program to get the File Extension in Java
package com.IPAddress;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.google.common.io.Files;
public class FileExtension {
public static void main(String args[]) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String s1 = bf.readLine();
System.out.print(Files.getFileExtension(s1));
}
}
Output
Thus, in this way, we learned how to find the extension of files in Java.