Example usage for java.nio.file Files newDirectoryStream

List of usage examples for java.nio.file Files newDirectoryStream

Introduction

In this page you can find the example usage for java.nio.file Files newDirectoryStream.

Prototype

public static DirectoryStream<Path> newDirectoryStream(Path dir) throws IOException 

Source Link

Document

Opens a directory, returning a DirectoryStream to iterate over all entries in the directory.

Usage

From source file:org.neo4j.io.fs.FileUtils.java

/**
 * Check if directory is empty.//from w ww.  jav a 2s  .  com
 * @param directory - directory to check
 * @return false if directory exists and empty, true otherwise.
 * @throws IllegalArgumentException if specified directory represent a file
 * @throws IOException if some problem encountered during reading directory content
 */
public static boolean isEmptyDirectory(File directory) throws IOException {
    if (directory.exists()) {
        if (!directory.isDirectory()) {
            throw new IllegalArgumentException("Expected directory, but was file: " + directory);
        } else {
            try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory.toPath())) {
                return !directoryStream.iterator().hasNext();
            }
        }
    }
    return true;
}

From source file:org.roda.core.storage.fs.FSUtils.java

/**
 * List content of the certain folder/*from w w  w .  j  a  v  a2  s . c  om*/
 * 
 * @param basePath
 *          base path
 * @param path
 *          relative path to base path
 * @throws NotFoundException
 * @throws GenericException
 */
public static CloseableIterable<Resource> listPath(final Path basePath, final Path path)
        throws NotFoundException, GenericException {
    CloseableIterable<Resource> resourceIterable;
    try {
        final DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path);
        final Iterator<Path> pathIterator = directoryStream.iterator();
        resourceIterable = new CloseableIterable<Resource>() {

            @Override
            public Iterator<Resource> iterator() {
                return new Iterator<Resource>() {

                    @Override
                    public boolean hasNext() {
                        return pathIterator.hasNext();
                    }

                    @Override
                    public Resource next() {
                        Path next = pathIterator.next();
                        Resource ret;
                        try {
                            ret = convertPathToResource(basePath, next);
                        } catch (GenericException | NotFoundException | RequestNotValidException e) {
                            LOGGER.error(
                                    "Error while list path " + basePath + " while parsing resource " + next, e);
                            ret = null;
                        }

                        return ret;
                    }

                };
            }

            @Override
            public void close() throws IOException {
                directoryStream.close();
            }
        };

    } catch (NoSuchFileException e) {
        throw new NotFoundException("Could not list contents of entity because it doesn't exist: " + path, e);
    } catch (IOException e) {
        throw new GenericException("Could not list contents of entity at: " + path, e);
    }

    return resourceIterable;
}

From source file:dk.dma.ais.downloader.QueryService.java

/**
 * Returns if the directory is empty or not
 * @param directory the directory to check
 * @return if the directory is empty or not
 *///www .  java  2s.c  o m
private static boolean isDirEmpty(final Path directory) throws IOException {
    try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(directory)) {
        return !dirStream.iterator().hasNext();
    } catch (Exception e) {
        // Should never happen
        return false;
    }
}

From source file:org.apache.taverna.databundle.TestDataBundles.java

protected List<String> ls(Path path) throws IOException {
    List<String> paths = new ArrayList<>();
    try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
        for (Path p : ds) {
            paths.add(p.getFileName() + "");
        }//from  ww  w.  j  av  a  2s.c  om
    }
    Collections.sort(paths);
    return paths;
}

From source file:org.roda.core.storage.fs.FSUtils.java

public static Long countPath(Path directoryPath) throws NotFoundException, GenericException {
    Long count = 0L;//from   www  .  j av a2  s.  c  om
    DirectoryStream<Path> directoryStream = null;
    try {
        directoryStream = Files.newDirectoryStream(directoryPath);

        final Iterator<Path> pathIterator = directoryStream.iterator();
        while (pathIterator.hasNext()) {
            count++;
            pathIterator.next();
        }

    } catch (NoSuchFileException e) {
        throw new NotFoundException(
                "Could not list contents of entity because it doesn't exist: " + directoryPath);
    } catch (IOException e) {
        throw new GenericException("Could not list contents of entity at: " + directoryPath, e);
    } finally {
        IOUtils.closeQuietly(directoryStream);
    }

    return count;
}

From source file:org.metaeffekt.dcc.controller.execution.RemoteExecutor.java

private void addDirectoryContentsToZipFile(Path rootDirectory, Path directory, ZipOutputStream zipFile,
        Set<String> includes) throws IOException {
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) {
        for (Path path : directoryStream) {
            Path relativePath = rootDirectory.relativize(path);
            if (CollectionUtils.isEmpty(includes) || includes.contains(relativePath.toString())) {
                if (Files.isDirectory(path)) {
                    String name = relativePath.toString();
                    name = name.endsWith("/") ? name : name + "/";
                    zipFile.putNextEntry(new ZipEntry(replaceWindowsPathSeparator(name)));
                    addDirectoryContentsToZipFile(rootDirectory, path, zipFile, null);
                } else {
                    String name = relativePath.toString();
                    zipFile.putNextEntry(new ZipEntry(replaceWindowsPathSeparator(name)));
                    try (InputStream fileToBeZipped = new BufferedInputStream(Files.newInputStream(path))) {
                        IOUtils.copy(fileToBeZipped, zipFile);
                    }/*from  w w w .  ja va 2  s .  co m*/
                }
            }
        }
    }
}

From source file:org.cryptomator.cryptofs.CryptoFileSystemImpl.java

void copy(CryptoPath cleartextSource, CryptoPath cleartextTarget, CopyOption... options) throws IOException {
    if (cleartextSource.equals(cleartextTarget)) {
        return;//from  www.  j a  v  a2  s .  co m
    }
    Path ciphertextSourceFile = cryptoPathMapper.getCiphertextFilePath(cleartextSource,
            CiphertextFileType.FILE);
    Path ciphertextSourceDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextSource,
            CiphertextFileType.DIRECTORY);
    if (Files.exists(ciphertextSourceFile)) {
        // FILE:
        Path ciphertextTargetFile = cryptoPathMapper.getCiphertextFilePath(cleartextTarget,
                CiphertextFileType.FILE);
        Files.copy(ciphertextSourceFile, ciphertextTargetFile, options);
    } else if (Files.exists(ciphertextSourceDirFile)) {
        // DIRECTORY (non-recursive as per contract):
        Path ciphertextTargetDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextTarget,
                CiphertextFileType.DIRECTORY);
        if (!Files.exists(ciphertextTargetDirFile)) {
            // create new:
            createDirectory(cleartextTarget);
        } else if (ArrayUtils.contains(options, StandardCopyOption.REPLACE_EXISTING)) {
            // keep existing (if empty):
            Path ciphertextTargetDir = cryptoPathMapper.getCiphertextDirPath(cleartextTarget);
            try (DirectoryStream<Path> ds = Files.newDirectoryStream(ciphertextTargetDir)) {
                if (ds.iterator().hasNext()) {
                    throw new DirectoryNotEmptyException(cleartextTarget.toString());
                }
            }
        } else {
            throw new FileAlreadyExistsException(cleartextTarget.toString());
        }
        if (ArrayUtils.contains(options, StandardCopyOption.COPY_ATTRIBUTES)) {
            Path ciphertextSourceDir = cryptoPathMapper.getCiphertextDirPath(cleartextSource);
            Path ciphertextTargetDir = cryptoPathMapper.getCiphertextDirPath(cleartextTarget);
            copyAttributes(ciphertextSourceDir, ciphertextTargetDir);
        }
    } else {
        throw new NoSuchFileException(cleartextSource.toString());
    }
}

From source file:me.ryandowling.allmightybot.AllmightyBot.java

private void loadUserChatLogs() {
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Utils.getUsersDir())) {
        for (Path path : directoryStream) {
            if (Files.isDirectory(path)) {
                String username = path.getFileName().toString();
                if (Files.exists(Utils.getUserChatFile(username))) {
                    Type type = new TypeToken<List<ChatLog>>() {
                    }.getType();/*w  ww .j  av  a2  s .  c om*/
                    List<ChatLog> chatMessages = GSON.fromJson(
                            FileUtils.readFileToString(Utils.getUserChatFile(username).toFile()), type);
                    this.userLogs.put(username, chatMessages);
                }
            }
        }
    } catch (IOException ex) {
    }
}

From source file:org.tinymediamanager.core.movie.entities.Movie.java

/**
 * Searches for actor images, and matches them to our "actors", updating the thumb url
 * /* w w w .j ava2 s . c  o m*/
 * @deprecated thumbPath is generated dynamic - no need for storage
 */
@Deprecated
public void findActorImages() {
    if (MovieModuleManager.MOVIE_SETTINGS.isWriteActorImages()) {
        // get all files from the actors path
        try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(getPathNIO())) {
            for (Path path : directoryStream) {
                if (Utils.isRegularFile(path)) {

                    for (MovieActor actor : getActors()) {
                        if (StringUtils.isBlank(actor.getThumbPath())) {
                            // yay, actor with empty image
                            String name = actor.getNameForStorage();
                            if (name.equals(path.getFileName().toString())) {
                                actor.setThumbPath(path.toAbsolutePath().toString());
                            }
                        }
                    }

                }
            }
        } catch (IOException ex) {
        }
    }
}

From source file:tachyon.java.manager.JavaFxManager.java

private void deepDelete(Path fe) {
    if (!Files.exists(fe)) {
        return;//from   w ww  .  j a  v a  2 s  . c om
    }
    if (Files.isDirectory(fe)) {
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(fe)) {
            for (Path run : stream) {
                deepDelete(run);
            }
        } catch (IOException | DirectoryIteratorException x) {
        }
        try {
            Files.delete(fe);
        } catch (IOException ex) {
        }
    } else {
        try {
            Files.delete(fe);
        } catch (IOException ex) {
        }
    }
}