Sample Programs

Sample java program to get the file extension


 package org.jusfortechies.example; 

 import java.io.File;
 
 public class GetFileExtension {

   public static void main(String[] args) {
      
      String SEPARATOR = ".";

      File file = new File("data.txt");

      String extension = "";

      if (file == null) {
         System.out.println("File not found");

      } else {

         String name = file.getName();

         // get file extension
         int extIndex = name.lastIndexOf(SEPARATOR);
         if (extIndex != -1) {
            extension = name.substring(extIndex + 1);
         }

         System.out.println("File Extension = " + extension);
      }
      
   }

 }




blog comments powered by Disqus