Home Java Java-get-example Java example program to get extension



Java example program to get extension
Posted on: October 24, 2008 at 12:00 AM
To get the file name and file extension separately we can do the string manipulation on the file name. Suppose "filename.ext" is some filename with extension is provided.

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 "filename.ext" is some filename with extension is provided. To get the filename and extension we will be calculating the last index of "." and will split the file name from this ".", since characters after this "." represents the extension of the file. GetFileExtension.java is as follows:

 

GetFileExtension.java

import java.util.*;
import java.lang.*;

public class GetFileExtension
{
  public static void main(String args[]) {
  String fileName = args[0];
  String fname="";
  String ext="";
  int mid= fileName.lastIndexOf(".");
  fname=fileName.substring(0,mid);
  ext=fileName.substring(mid+1,fileName.length());  
  System.out.println("File name ="+fname);
  System.out.println("Extension ="+ext);  
  }
}

Output:

C:\javaexamples>javac GetFileExtension.java

C:\javaexamples>java GetFileExtension document.doc
File name =document
Extension =doc

Download Source Code

Related Tags for Java example program to get extension:
cstringfileideiogetextensionipvinameidfilenamemanipulationwithtoposexteilitcanulmaninmpartrseparateoscaosmeproppratedossusoatisandarstrxtssriringthstatiprndonomo


More Tutorials from this section

Ask Questions?    Discuss: Java example program to get extension   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.