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 get file permission
java get file permission  How to check the get file permission in Java
Advertisements
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
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
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
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 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 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
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 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
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 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 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 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
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 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
In Java how to get size of class and object?
In Java how to get size of class and object?  In Java how to get size of class and object
In Java how to get size of class and object?
In Java how to get size of class and object?  In Java how to get size of class and object
How to get UTF-8 working in java webapps?
How to get UTF-8 working in java webapps?  How to get UTF-8 working in java webapps
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
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
how to get java path name
How to write script in Java  <html> <head> <script>alert('ok')</script> </head> </Html>
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

Ads