CONCEPT

First file should be created.
In this example we just use a pdf file from desktop location. Then we get the name of file. getName() is the method to get file name and it returns the file name as string. so we store the name in string object filename. Another method which is used to break a string into substring and this method have two parameter Start index and End Index both parameter should be integer type. then we shoould have break filename from (".") +1 because extention is start after dot(.) +1 and last index number should be  length of the filename string.

Syntax

File file=new File("C:/Users/vishal dixit/Desktop/app.pdf");

String filename=file.getName();

String extention=filename.substring(filename.lastIndexOf('.')+1, filename.length());

System.out.println(extention);

OUTPUT

pdf