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);
}
Use String#substring and String#lastIndexOf:
filename.substring(filename.lastIndexOf("."),filename.length())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With