Check file extension in Java

后端 未结 8 955
深忆病人
深忆病人 2021-01-01 10:39

I have to import data from an Excel file to database and to do this, I would like to check the extension of the chosen file.

This is my code:

String          


        
8条回答
  •  长情又很酷
    2021-01-01 10:47

    You can use Apache Commons Api to check the file extension

    String filename = file.getName();
    if(!FilenameUtils.isExtension(filename,"xls")){
      JOptionPane.showMessageDialog(null, "Choose an excel file!");
    }
    

    http://commons.apache.org/io/api-release/index.html?org/apache/commons/io/package-summary.html

提交回复
热议问题