how to get file extension in java

Ads
 

how to get file extension in java

Hi,

I want to get the file extension in Java.

how to get file extension in java?

Thanks

View Answers

August 2, 2018 at 5:52 AM

Hi,

Here is sample code to get file extension in Java:

package examples;

import java.io.File;

public class GetFileExtension {

    public static void main(String[] args) {
      File file = new File("c:/Deepak/1.jpg");
       String extension="";
        try {
            if (file != null && file.exists()) {
                String name = file.getName();
                extension = name.substring(name.lastIndexOf(".")+1);
            }
        } catch (Exception e) {
            extension = "";
        }
        System.out.println("File Exentsion:"+extension);
    }

}

Thanks


August 2, 2018 at 5:56 AM

Hi,

You can also use:

String extension = FilenameUtils.getExtension(file.getName());

Thanks


August 2, 2018 at 6:39 AM

Hi,

Here is example using FilenameUtils.getExtension(file.getName())

package examples;

import java.io.File;

import org.apache.commons.io.FilenameUtils;

public class GetFileExtension2 {

    public static void main(String[] args) {
      File file = new File("c:/Deepak/1.jpg");
      String extension = FilenameUtils.getExtension(file.getName());
      System.out.println("File Exentsion:"+extension);
    }

}

Thanks

Ads









Related Tutorials/Questions & Answers:
how to get file extension in java
how to get file extension in java  Hi, I want to get the file extension in Java. how to get file extension in java? Thanks   Hi, Here is sample code to get file extension in Java: package examples; import
Java Display File-Extension
Java Display File-Extension   ... the file extension of the file. Here you will be asked to enter the file name and then you will get the extension of the file. Description of the code
Advertisements
java get file permission
java get file permission  How to check the get file permission in Java
how to open a file of .110 extension??
how to open a file of .110 extension??  I have a file which is in .110 extension and that file is belongs to Nokia 1100 mobile. How to open that kind of file format? Do i need any converter to open that? So that i can understand
Java Change File-Extension
Java Change File-Extension       In this program you will learn how to change the file extension... is to be changed and then you will get the file name with the changed extension
Java example program to get extension
Java example program to get extension       java get extension To get the file name and file extension separately we can do the string manipulation on the file name. Suppose
How to get of lastmodified file list between two times in java?
How to get of lastmodified file list between two times in java?  Hi... times like get the last modified file bet 02:00:00am and 01:00:00am (so...-dd"); } public boolean accept(File dir, String name) { Date d = new
how to get description for each file
how to get description for each file  hi, how to get description for each file using php or java script..... condtion:two text boxes for each file if the user select 10 file to upload........get the description
Java get Filename without Extension
Java get Filename without Extension       In this section, you will learn how to obtain the filename without extension. For this, the file name or the directory name
How to get path of a file in iOS?
How to get path of a file in iOS?  Hi iOS project I have added a file abg.jpg, Now I want to know the path of the image file when installed in iOS. Pl let's know the code. Thanks   Hi, You can use following code
How to get Java SDK
How to get Java SDK  Hi, I have purchased a new computer and installed windows 7. Now I have to get the Java SDK and install on my computer for developing and testing Java programs. So, can anyone tell me How to get Java SDK
Java get File Type
Java get File Type       This section illustrates you how to get the file type. In the given example, we have create an instance of the class File and passed the file 'Hello.txt
How to write in File in Java
How to write in File in Java  Hi, How to write in File in Java. Please suggest and give example of this program. thanks
How to rename a file in Java?
How to rename a file in Java?  Hi, What is the code for renaming a file? How to rename a file in Java? Thanks   Hi, You can use... the File or Directory in Java. Thanks
how to get this xml syntx in java??????
how to get this xml syntx in java??????  how to get this xml syntx in java?????? < comp xlink:type="new" xlink:actuate="onload"/>
how to get this xml syntx in java??????
how to get this xml syntx in java??????   how to get this xml syntx in java?????? < comp xlink:type="new" xlink:actuate="onload"/>
How to compress a file in Java?
How to compress a file in Java?  How to compress a file in Java through Java code? I want a good example in Java. Thanks   Hi, Check the tutorial Compressing the file into GZIP format. Thanks
How to get the current date in Java?
How to get the current date in Java?  Hi, I am new to Java and trying to write a program to get the current date. How to get the current date in Java? Thanks   Hi, Check the tutorial: Java program to get current date
Java get byte array from file
Java get byte array from file  what is the code example in Java for getting byte array from file in memory. Thanks   This is very simple code in Java. Following tutorials will teach you how to achieve this. Java
How to write in File in Java
How to write in File in Java  Hi, How to write in File in Java... bufferedWriter object to write in the file. For more details and examples related to How to write in File of Java Program
How to get enum value in Java
How to get enum value in Java  Hi all, i am looking for a solution to get an enum value from index in Java. Please suggest. Thanks
How to Write to file in Java?
How to Write to file in Java?  How to Write to file in Java Program?   Hi, For writing a file in Java, we need some classes from Java.IO... the Examples of How to write to File in Java Program: WriteToFileExample.java import
Detecting the file extension
Detecting the file extension   How to detect the file extension in PHP?   Read the given tutorial to detect the file extension in PHP. The fileexists() function, fileexists php, file_exists in php
How to get current timestamps in Java
How to get current timestamps in Java  Hi, I want to get the current Timestamp in Java and then use it in JDBC program. How to get current timestamps in Java? Thanks   Hi, In Java using get the current Timestamp
How to get length of string in Java?
How to get length of string in Java?  What is the method of String for getting the length? How to get length of string in Java? Tell me code fast... Tutorials"; int length = str.length(); You have to use the str.length() to get
How to get day of week in Java?
How to get day of week in Java?  Hi, How to get day of week in Java? Thanks   Hi, Calendar class is very useful in Java. You can use this class for getting the day of the week. Here is sample code: int weekday
how to get browser cookies in java
how to get browser cookies in java  Hi, How to get cookies in serverlet and then print? how to get browser cookies in java? Thanks   Hi, Use following code: Cookie[] cookies = request.getCookies(); for (int i = 0
Java get XML File
Java get XML File       In this section, you will study how the java application read the XML file. For this, you need to create a XML file. Here is the employee.xml
How to get xml file form http port using web service
How to get xml file form http port using web service  hi I am suresh... and use the xml data to convert it to java file and store it to DB. But i am struck with getting xml file. How to access xml file in web service? Please Help
How to delete file in Java code?
How to delete file in Java code?  Hi, From my Java program I have to delete a file. How to delete file in Java code? Thanks   Hi, Java... complete example code at How to delete file in Java?. Thanks   Hi, Also
Setting file extension
Setting file extension  Hi, I have used FileChooser to save a file. When I was trying to save it, the extension of the file is showing 'File'. How can I save that file to .xls ? Thanks

Ads