Java Path File Check nio isDirectoryEmpty(Path directory)

Here you can find the source of isDirectoryEmpty(Path directory)

Description

Check if the directory is empty.

License

Open Source License

Parameter

Parameter Description
directory directory to check

Return

true when empty

Declaration

public static boolean isDirectoryEmpty(Path directory) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w w  w  .  j ava  2  s.  c o m*/
 * This file is part of DiscoSync (home: github.com, leitwolf7/discosync)
 *
 * Copyright (C) 2015, 2015 leitwolf7
 *
 *  DiscoSync is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  DiscoSync is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with DiscoSync.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;
import java.nio.file.*;

public class Main {
    /**
     * Check if the directory is empty.
     *
     * @param directory directory to check
     * @return true when empty
     */
    public static boolean isDirectoryEmpty(Path directory) throws IOException {
        boolean retval = true;
        DirectoryStream<Path> dirStream = Files.newDirectoryStream(directory);
        if (dirStream.iterator().hasNext()) {
            retval = false;
        }
        dirStream.close();
        return retval;
    }
}

Related

  1. isBinary(Path file)
  2. isContained(Path contained, Path container)
  3. isDirectory(Path fileOrDir, LinkOption... options)
  4. isDirectory(Path value)
  5. isDirectoryEmpty(Path dir)
  6. isDirectoryEmpty(Path path)
  7. isDirectoryEmpty(String dirPath)
  8. isDirectoryInPath(File p, File d)
  9. isDirEmpty(final Path directory)