如何在 JTextField 中显示附加文件的大小
How to show size of attached file in JTextField
如何在我的 JTextField 中显示我从 JFileChooser 选择的文件的大小?当我使用 f.getTotalSpace()
时,系统显示我的 PC 的总磁盘 space,这不是我想要的。
private void jButtonAttachActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser jc = new JFileChooser();
jc.setDialogType(JFileChooser.OPEN_DIALOG);
jc.showOpenDialog(null);
File f = jc.getSelectedFile();
if (f.length() / 1024 < 1024) {
jTextFieldAttachments.setText(f.getName() + " " + "(" + f.length() / 1024 + " KB)");
}
else if (f.length() / 1024 >= 1024) {
jTextFieldAttachments.setText(f.getName() + " " + "(" + f.length() / 1048576 + " MB)");
}
}
**EDIT by @RoeyGolzarpoor**
if (f.length() / 1024 <= 1024) {
jTextFieldAttachments.setText(f.getName() + " " + "(" + String.format("%.1f", (f.length() / 1024)) + " KB)");
}
else if (f.length() / 1024 > 1024) {
jTextFieldAttachments.setText(f.getName() + " " + "(" + String.format("%.1f", (f.length() / 1048576)) + " MB)");
}
错误
使用:
float kilo_bytes f.length()/1024;
Float.parseFloat(String.format("%.1f", kilo_bytes));
这将 return 您的文件 KB
温馨提示:
1024 字节 = 1 千字节
1024 kb = 1 兆字节
1024 mb = 1 Gega 字节
1024 GB = 1 太字节
如何在我的 JTextField 中显示我从 JFileChooser 选择的文件的大小?当我使用 f.getTotalSpace()
时,系统显示我的 PC 的总磁盘 space,这不是我想要的。
private void jButtonAttachActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser jc = new JFileChooser();
jc.setDialogType(JFileChooser.OPEN_DIALOG);
jc.showOpenDialog(null);
File f = jc.getSelectedFile();
if (f.length() / 1024 < 1024) {
jTextFieldAttachments.setText(f.getName() + " " + "(" + f.length() / 1024 + " KB)");
}
else if (f.length() / 1024 >= 1024) {
jTextFieldAttachments.setText(f.getName() + " " + "(" + f.length() / 1048576 + " MB)");
}
}
**EDIT by @RoeyGolzarpoor**
if (f.length() / 1024 <= 1024) {
jTextFieldAttachments.setText(f.getName() + " " + "(" + String.format("%.1f", (f.length() / 1024)) + " KB)");
}
else if (f.length() / 1024 > 1024) {
jTextFieldAttachments.setText(f.getName() + " " + "(" + String.format("%.1f", (f.length() / 1048576)) + " MB)");
}
错误
使用:
float kilo_bytes f.length()/1024;
Float.parseFloat(String.format("%.1f", kilo_bytes));
这将 return 您的文件 KB
温馨提示:
1024 字节 = 1 千字节
1024 kb = 1 兆字节
1024 mb = 1 Gega 字节
1024 GB = 1 太字节