用Java检查文件扩展名 [英] Check file extension in Java
本文介绍了用Java检查文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须将数据从Excel文件导入数据库并执行此操作,我想检查所选文件的扩展名。
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.
这是我的代码:
String filename = file.getName();
String extension = filename.substring(filename.lastIndexOf(".") + 1, filename.length());
String excel = "xls";
if (extension != excel) {
JOptionPane.showMessageDialog(null, "Choose an excel file!");
}
else {
String filepath = file.getAbsolutePath();
JOptionPane.showMessageDialog(null, filepath);
String upload = UploadPoData.initialize(null, filepath);
if (upload == "OK") {
JOptionPane.showMessageDialog(null, "Upload Successful!");
}
}
但我总是得到:
选择一个excel文件!
Choose an excel file!
我找不到什么我的代码有问题,请有人帮忙。
I can't find what is wrong with my code, could someone please help.
推荐答案
以下
extension != excel
应该是
!excel.equals(extension)
或
!excel.equalsIgnoreCase(extension)
参见
这篇关于用Java检查文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文