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

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









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
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
Advertisements
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:ADS
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
Java file extension
Java file extension In this section you will learn how to display the file extension of the file. Description of code: Files are usually known through their extensions. An extension differentiates the file from another file
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 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
java get file permission
java get file permission  How to check the get file permission in Java
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 filename without extension
Java filename without extension In his section, you will learn how to get the file name without extension. Description of code: In one of the previous sections, we have discussed the extraction of file extension from the given file
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 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
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
Java file get name
Java file get name In this section, you will learn how to get the name of the file. Description of code: You can see in the given example, we have created... class, you can get the name of any file. Output:ADS_TO_REPLACE_2
Java file get size
Java file get size In this section, you will learn how to get the size of a file. Description of code: You can see in the given example, we have created an object of File class and specified a file in the constructor. The object
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 Get the Uploaded File Information in the Receiving Script?
How To Get the Uploaded File Information in the Receiving Script?  How To Get the Uploaded File Information in the Receiving Script
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
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
Java Get File Name
Java Get File Name       In this section, you will study how to obtain the name of file.  In order to get the file name, we have provided the path of file 'Hello.txt
Java file get parent directory
Java file get parent directory In this section, you will learn how to get the parent directory of the file. Description of code: The package java.io.* package works with both files and directories. File object represents both file
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
parsing XML file to get java object - XML
parsing XML file to get java object  Hello, I'm facing a problem in parsing XML file to get the java object. I've tried to retrieve data from XML file using SAX parser. my XML file structure is the following
Java Get Last Line of File
Java Get Last Line of File     ... you in understanding Get Last Line of File in Java . For this we have a class name "Get Last Line of File". Inside the main method we create an instance
How could we get log file as simple text format
How could we get log file as simple text format  Above given javaerrorlog creates the log file in xml code how could we get it in simple text Manmohan
Java get File List
Java get File List       In this section, you will study how to retrieve all the files... the directory 'C:\\" to the constructor of class File.  file.listFiles
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 get the full path location using <input type=file>
How to get the full path location using input type=file  Hi. I have used input type=file> in HTML to select an image. But it selects only the image but not with its full path location. how to do it? Thanks in advance
JDOM Element Example, How to get text of element of xml file.
JDOM Element Example, How to get text of element of xml file. In this tutorial, we will see how to get text value of an element from xml file by using JDOM toolkit. JDOM is a library used for manipulating xml file
How to get the browsed file path into controller bean using JSF icefaces
How to get the browsed file path into controller bean using JSF icefaces ... that is C:\Documents and Settings\personInfo.xml how can I get the path? Is there any other way to browse the file and get the path of the file into controllerbean
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 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 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 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
JDOM Element example, How to get root element of xml file.
JDOM Element example, How to get root element of xml file. In this tutorial, we will see how to get root element of xml file. Code. Student.xml...;File XmlFile = new File("Student.xml");   
Java get File Timestamp
Java get File Timestamp       In this section, you will study how to obtain the file... of the specified file. The class Runtime provides the runtime environment. The class
How to get full path of the uploaded file using commons fileupload jar file
How to get full path of the uploaded file using commons fileupload jar file  I want to upload file to database with its full path. Because of the security reasons, the browsers provide only the file name when using normal file
Java file get free space
Java file get free space In this section, you will learn how to find the free space of any file or directory. Description of code: JDK 1.6 provides few new... space.ADS_TO_REPLACE_1 In the given example, we have created an instance of a File
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
How to Get Started with Java?
A Java Beginners Tutorial - How to Get Started with Java? For a programmer... How You Get to Work with Java? Now, this is where you aspired to come... is saved with a file extension mentioning .java. The next step is compiling which means
Which extension i use for save hibernate file.
Which extension i use for save hibernate file.   Please give me answer that which extension i use for save the hibernate file and how i run that file as well as complie that file.   hi friend, Naming convention
How to copy a file in java
How to copy a file in java In this section you will learn about how to copy a content of one file to another file. In java, File API will not provide any.... In another way we can copy using File I/o Stream. Example:  Java Code to copy
How to make a file in java
Description: This example demonstrate how to make a file and write string in it. To create a file in java we use the File Class. The instance of the File class is immutable that means the pathname represented by the File object
How to Read a File in Java
How to Read a File in Java? In this section we are going to know, How to read a file in Java. We have to follow three step to read a File. First get...: Java File Class represent the File and Directory pathname in a abstract
Java file get total space
Java file get total space In this section, you will learn how to get the total... In the given example, we have created an instance of a File to represent a disc of our file system. Then we have called the method getTotalSpace() of class
How to delete file in java ?
How to delete file in java ? In this section we will learn how to write a program to delete a file in java. You can delete the file or folder, java File..._TO_REPLACE_1 Now, here is the code to delete a file in java. import
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
How to write into CSV file in Java
Object to PrintWriter instance. How to create File to CSV in Java...How to write into CSV file in Java  How to write into CSV file in Java   Hi, To create Comma separated value(CSV) file is very simple
How to write file by line in Java
How to write file by line in Java  How to write file by line in Java   Hi, For Write to a file by line using java programming language we... in a new line. How to use Write To File By Line in Java programs

Ads