package com.candidjava.file; import java.io.File; public class FileExtension { public static void main(String[] args) { File file = new File("demo.txt"); String name = file.getName(); String fileExtension; if ((name.lastIndexOf(".") != -1) && (name.lastIndexOf(".")) != 0) { fileExtension = name.substring(name.lastIndexOf(".") + 1); System.out.println("File Extension is:" + fileExtension); } else { System.out.println("Invalid file format"); } } }