简体   繁体   English

Java-JFileChooser和文件大小

[英]Java - JFileChooser & file size

I am trying to get the size of a file from JFileChooser so that I can print it in table, how do I do that? 我正在尝试从JFileChooser获取文件的大小,以便可以在表中打印它,该怎么做?

JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

String[] fileNames = fileChooser.getSelectedFile().list();
int fileSize;

for (int i = 0; i < files.length; i++) {

    int fileSize = fileNames[i] //How do I get the size of the file here?
    model.addRow(new Object[]{fileNames[i], fileSize});

}

Get a list of files (instead of file names) and then just call "length()" on them: 获取文件列表(而不是文件名),然后对它们调用“ length()”:

File[] files = fileChooser.getSelectedFile().listFiles();
for (int i = 0; i < files.length; i++) {

    int fileSize = files[i].length();
    model.addRow(new Object[]{fileNames[i], fileSize});

}

You can use 您可以使用

File[] files = fileChooser.getSelectedFile().listFiles();

and then use the for loop with the method length() from the File class to get the file size, ie, 然后使用File类中的方法length()的for循环来获取文件大小,即

int fileSize = files[i].length();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM