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:PatternFinder.java

private List<String> getFileNames(String search, List<String> fileNames, Path dir) {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
        for (Path path : stream) {
            if (path.toFile().isDirectory()) {
                getFileNames(search, fileNames, path);
            } else {

                String fileName = path.toAbsolutePath().getFileName().toString();
                String[] data = search.split("-");
                if (data[0] != null && (data[0].equals("atoms") || data[0].equals("molecules")
                        || data[0].equals("organisms")))
                    data = (String[]) ArrayUtils.remove(data, 0);

                String differentSearchTerm = String.join("-", data).toLowerCase();

                //Search for patternlab identifier in filesystem
                if (fileName.contains(differentSearchTerm) || fileName.contains(search)) {
                    fileNames.add(path.toAbsolutePath().toString());

                }/*from w w w  .  j a  v  a2 s.c  o  m*/

            }
        }
    } catch (IOException e) {
        //e.printStackTrace();
    }
    return fileNames;
}

From source file:ddf.security.samlp.MetadataConfigurationParser.java

private void parseEntityDescriptions(List<String> entityDescriptions) throws IOException {
    String ddfHome = System.getProperty("ddf.home");
    for (String entityDescription : entityDescriptions) {
        buildEntityDescriptor(entityDescription);
    }/*from www. j a  v a 2s .  co  m*/
    Path metadataFolder = Paths.get(ddfHome, ETC_FOLDER, METADATA_ROOT_FOLDER);
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(metadataFolder)) {
        for (Path path : directoryStream) {
            if (Files.isReadable(path)) {
                try (InputStream fileInputStream = Files.newInputStream(path)) {
                    EntityDescriptor entityDescriptor = readEntityDescriptor(
                            new InputStreamReader(fileInputStream, "UTF-8"));

                    LOGGER.error("parseEntityDescriptions:91 entityId = {}", entityDescriptor.getEntityID());
                    entityDescriptorMap.put(entityDescriptor.getEntityID(), entityDescriptor);
                    if (updateCallback != null) {
                        updateCallback.accept(entityDescriptor);
                    }
                }
            }
        }
    } catch (NoSuchFileException e) {
        LOGGER.debug("IDP metadata directory is not configured.", e);
    }
}

From source file:org.digidoc4j.ContainerTest.java

@AfterClass
public static void deleteTemporaryFiles() {
    try {//from w  w  w.  j  a  va  2s .co  m
        DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get("."));
        for (Path item : directoryStream) {
            String fileName = item.getFileName().toString();
            if ((fileName.endsWith("bdoc") || fileName.endsWith("ddoc")) && fileName.startsWith("test"))
                Files.deleteIfExists(item);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:TypeServiceTest.java

@Test
public void testCreateBackup() {
    typeService.save("type-test.xml", storedType());
    simulateWait();/*from   w ww.  j  a  v  a2 s  .co m*/
    typeService.save("type-test.xml", storedType());

    try (DirectoryStream<Path> s = Files.newDirectoryStream(fileSystemProvider.getFileSystem().getPath("/"))) {
        Iterable<Path> iterable = () -> s.iterator();

        Assert.assertEquals(2L, StreamSupport.stream(iterable.spliterator(), false)
                .filter(p -> p.getFileName().toString().startsWith("type-test")).count());
    } catch (IOException ex) {
        log.fatal(ex);
    }
}

From source file:au.org.ands.vocabs.toolkit.provider.transform.GetMetadataTransformProvider.java

/**
 * Parse the files harvested from PoolParty and extract the
 * metadata./*  www .j a  v a  2 s . c  o  m*/
 * @param pPprojectId The PoolParty project id.
 * @return The results of the metadata extraction.
 */
public final HashMap<String, Object> extractMetadata(final String pPprojectId) {
    Path dir = Paths.get(ToolkitFileUtils.getMetadataOutputPath(pPprojectId));
    HashMap<String, Object> results = new HashMap<String, Object>();
    ConceptHandler conceptHandler = new ConceptHandler();
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
        for (Path entry : stream) {
            conceptHandler.setSource(entry.getFileName().toString());
            RDFFormat format = Rio.getParserFormatForFileName(entry.toString());
            RDFParser rdfParser = Rio.createParser(format);
            rdfParser.setRDFHandler(conceptHandler);
            FileInputStream is = new FileInputStream(entry.toString());
            logger.debug("Reading RDF:" + entry.toString());
            rdfParser.parse(is, entry.toString());
        }
    } catch (DirectoryIteratorException | IOException | RDFParseException | RDFHandlerException ex) {
        results.put(TaskStatus.EXCEPTION, "Exception in extractMetadata while Parsing RDF");
        logger.error("Exception in extractMetadata while Parsing RDF:", ex);
        return results;
    }
    results.putAll(conceptHandler.getMetadata());
    results.put("concept_count", Integer.toString(conceptHandler.getCountedConcepts()));
    return results;
}

From source file:org.wte4j.examples.showcase.server.hsql.ShowCaseDbInitializerTest.java

private Set<String> listFiles(Path directory) throws IOException {
    Set<String> fileNamesInDirectory = new HashSet<String>();
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) {
        for (Path path : directoryStream) {
            fileNamesInDirectory.add(path.getFileName().toString());
        }/*from  w  ww  . j  ava2 s . co  m*/
    }
    return fileNamesInDirectory;
}

From source file:org.cryptomator.frontend.webdav.servlet.DavFolder.java

@Override
public DavResourceIterator getMembers() {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
        List<DavResource> children = new ArrayList<>();
        for (Path childPath : stream) {
            BasicFileAttributes childAttr = Files.readAttributes(childPath, BasicFileAttributes.class);
            DavLocatorImpl childLocator = locator.resolveChild(childPath.getFileName().toString());
            if (childAttr.isDirectory()) {
                DavFolder childFolder = factory.createFolder(childLocator, childPath, Optional.of(childAttr),
                        session);/*ww  w  .  j  av a2 s.co  m*/
                children.add(childFolder);
            } else if (childAttr.isRegularFile()) {
                DavFile childFile = factory.createFile(childLocator, childPath, Optional.of(childAttr),
                        session);
                children.add(childFile);
            }
        }
        return new DavResourceIteratorImpl(children);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:org.azyva.dragom.util.Util.java

public static boolean isDirectoryEmpty(Path path) {
    DirectoryStream<Path> directoryStream;

    try {/*from   ww  w  .j av  a 2  s. c  o m*/
        directoryStream = Files.newDirectoryStream(path);
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }

    return !directoryStream.iterator().hasNext();
}

From source file:org.wso2.carbon.inbound.localfile.LocalFileOneTimePolling.java

/**
 * Before start the watch directory if watchedDir has any files it will start to process.
 *///from w w  w  . j  a  v  a 2s.co  m
private void setProcessAndWatch() {
    Path dir = FileSystems.getDefault().getPath(watchedDir);
    DirectoryStream<Path> stream = null;
    try {
        stream = Files.newDirectoryStream(dir);
        for (Path path : stream) {
            processFile(path, contentType);
        }
    } catch (IOException e) {
        log.error("Error while processing the directory." + e.getMessage(), e);
    } finally {
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException e) {
            log.error("Error while close the DirectoryStream." + e.getMessage(), e);
        }
    }
    startWatch();
}

From source file:org.carcv.impl.core.io.FFMPEGVideoHandlerIT.java

/**
 * Test method for {@link org.carcv.impl.core.io.FFMPEGVideoHandler#splitIntoFrames(java.nio.file.Path, int)}.
 *
 * @throws IOException//from   www  . j a  v  a2s. com
 */
@Test
public void testSplitIntoFramesPathInt() throws IOException {
    FFMPEGVideoHandler fvh = new FFMPEGVideoHandler();
    FFMPEGVideoHandler.copyCarImagesToDir(entry.getCarImages(), videoDir);
    Path video = fvh.generateVideo(videoDir, FFMPEGVideoHandler.defaultFrameRate);

    assertTrue("Split failed.", fvh.splitIntoFrames(video, FFMPEGVideoHandler.defaultFrameRate));

    Path dir = Paths.get(video.toString() + ".dir");
    DirectoryStream<Path> paths = Files.newDirectoryStream(dir);
    int counter = 0;
    for (@SuppressWarnings("unused")
    Path p : paths) {
        counter++;
    }
    assertEquals(entry.getCarImages().size(), counter);

    Files.delete(video);
    DirectoryWatcher.deleteDirectory(dir);
}