Monday, April 18, 2011

How to check whether a directory is empty or not?

Following example gets the size of a directory by using file.isDirectory(),file.list() and file.getPath() methodsof File class.

File file = new File("/data");
      if (file.isDirectory()) {
         String[] files = file.list();
         if (files.length > 0) {
            System.out.println("The " + file.getPath() +
            " is not empty!");

No comments:

Post a Comment