whla
whla

Reputation: 811

Check if folder is empty without reference issues

The code below sorts an array of files that it reads from a directory. The only problem is that if the folder is empty then I get a null pointer exception. I have tried a few different ways of checking first if the folder is empty and then executing the below code, but they all used if statements which took this code out of scope. Is there a way I could check if the directory is empty and if it is skip over this code?

//sort array listOfFiles by time (oldest to newest)
    File folder = new File(dbBackupLocation + "/" + dbHost);
    File[] listOfFiles = folder.listFiles();
    Arrays.sort(listOfFiles, new Comparator<File>() {
        @Override
                    public int compare(File f1, File f2) {
            return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
        }
    });

Edit: NullPointerException on line Arrays.sort(listOfFiles, new Comparator<File>() {

I used Elliott Frisch code:

File folder = new File(dbBackupLocation + "/" + dbHost);
    File[] listOfFiles = folder.listFiles();
    if (listOfFiles != null && listOfFiles.length > 0) {
        // The array isn't empty.
        Arrays.sort(listOfFiles, new Comparator<File>() {
            @Override
            public int compare(File f1, File f2) {
                return Long.valueOf(f1.lastModified())
                        .compareTo(f2.lastModified());
            }
        });
    }

But when I try to use listOfFiles like: for(File s : listOfFiles) { i++; } I am apparently dereferencing it?

Upvotes: 0

Views: 1211

Answers (3)

Edwin Torres
Edwin Torres

Reputation: 2864

Check if the folder exists and if it is in fact a folder. Then check the size of listOfFiles:

File folder = new File(dbBackupLocation + "/" + dbHost);
if (folder != null && folder.exists() && folder.isDirectory()) {
    File[] listOfFiles = folder.listFiles();
    if (listOfFiles != null && listOfFiles.length > 0) {
        Arrays.sort(listOfFiles, new Comparator<File>() {
            @Override
            public int compare(File f1, File f2) {
                return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
            }
        });
    }

    //display the files
    for (File f : listOfFiles) {
        System.out.println(f);
    }
}

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201497

If I understand you, you want something like

File folder = new File(dbBackupLocation + "/" + dbHost);
File[] listOfFiles = folder.listFiles();
if (listOfFiles != null && listOfFiles.length > 0) {
  // The array isn't empty.
  Arrays.sort(listOfFiles, new Comparator<File>() {
    @Override
    public int compare(File f1, File f2) {
      return Long.valueOf(f1.lastModified())
          .compareTo(f2.lastModified());
    }
  });
}

Upvotes: 4

Devarsh Desai
Devarsh Desai

Reputation: 6112

Check the folder size via a command:

Runtime.getRuntime().exec("/bin/bash -c du /path/to/Michaels/folder");

The 'du' linux command is used for finding the size of a directory.

Please let me know if you have any questions!

Upvotes: 0

Related Questions