Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the type of selected file by JFileChooser

I am writing a program to select file from the JFileChooser. How can I get the file type (e.g. is that file is ".txt" or ".html" of ".dat", etc.. )

I have the following code. what should i have to add extra lines?

JFileChooser choice = new JFileChooser();
int option = choice.showOpenDialog(this);
if (option == JFileChooser.APPROVE_OPTION)
{
     String path=choice.getSelectedFile().getAbsolutePath();
     String filename=choice.getSelectedFile().getName();
     System.out.println(path);
     listModel.addElement(path);
     System.out.println(filename);
}
like image 631
Kundan Avatar asked Feb 03 '26 17:02

Kundan


1 Answers

Use String#substring and String#lastIndexOf:

filename.substring(filename.lastIndexOf("."),filename.length())

like image 105
Thinesh Ganeshalingam Avatar answered Feb 06 '26 05:02

Thinesh Ganeshalingam