0

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 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});

}
jadrijan
  • 1,438
  • 4
  • 31
  • 48

2 Answers2

3

Get a list of files (instead of file names) and then just call "length()" on them:

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});

}
Chris
  • 22,923
  • 4
  • 56
  • 50
2

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, i.e.,

int fileSize = files[i].length();
Boris
  • 7,054
  • 1
  • 15
  • 13