【发布时间】:2020-02-23 05:08:32
【问题描述】:
我正在使用 Jfilechooser,如果我选择文件,它将计算文件名的字符数,但是如果文件超过 3kb,它将显示 Joptionpane。我的问题是即使文件是 0kb,Joptionpane 也会出来,我不知道我的代码是否正确。
private int countWords(File f) {
int filelength = 0;
// Count of words.
filelength = f.getName().length();
double bytes = f.length();
double kilobytes = (bytes / 1024);
double limit = (1024 * 3);
if (f.exists() && (kilobytes >= limit)) {
JOptionPane.showConfirmDialog(null, "File Size:" + kilobytes + "KB", "Message Interrupted",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
}
return filelength;
}
【问题讨论】:
-
为什么不打印出字节或将其显示在 JOptionPane 中?