Example usage for java.nio.file Files list

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

Introduction

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

Prototype

public static Stream<Path> list(Path dir) throws IOException 

Source Link

Document

Return a lazily populated Stream , the elements of which are the entries in the directory.

Usage

From source file:alliance.docs.DocumentationTest.java

@Test
public void testBrokenAnchorsPresent() throws IOException, URISyntaxException {
    List<Path> docs = Files.list(getPath()).filter(f -> f.toString().endsWith(HTML_DIRECTORY))
            .collect(Collectors.toList());

    Set<String> links = new HashSet<>();
    Set<String> anchors = new HashSet<>();

    for (Path path : docs) {
        Document doc = Jsoup.parse(path.toFile(), "UTF-8", EMPTY_STRING);

        String thisDoc = StringUtils.substringAfterLast(path.toString(), File.separator);
        Elements elements = doc.body().getAllElements();
        for (Element element : elements) {
            if (!element.toString().contains(":")
                    && StringUtils.substringBetween(element.toString(), HREF_ANCHOR, CLOSE) != null) {
                links.add(thisDoc + "#" + StringUtils.substringBetween(element.toString(), HREF_ANCHOR, CLOSE));
            }/*from w  w w.  jav  a 2s. c o m*/

            anchors.add(thisDoc + "#" + StringUtils.substringBetween(element.toString(), ID, CLOSE));
        }
    }
    links.removeAll(anchors);
    assertThat("Anchors missing section reference: " + links.toString(), links.isEmpty());
}

From source file:org.silverpeas.core.util.file.FileFolderManager.java

/**
 * Returns all the files (and only the files, not the directories) that are directly inside the
 * given directory. Throws an {@link UtilException} if the
 * specified path doesn't denote a directory or if the listing of all of its files fails.
 * @param path the path of the directory
 * @return a collection of files//ww  w.j ava 2s . co m
 */
public static Collection<File> getAllFile(final String path) {
    final List<File> result;
    final Path directory = Paths.get(path);
    if (directory.toFile().isDirectory()) {
        try (final Stream<Path> folders = Files.list(directory)) {
            result = folders.filter(p -> p.toFile().isFile()).map(Path::toFile)
                    .sorted(new NameFileComparator(IOCase.INSENSITIVE)).collect(Collectors.toList());
        } catch (IOException e) {
            throw new UtilException(e);
        }
    } else {
        throw new UtilException(path + NOT_A_DIRECTORY_MSG);
    }
    return result;
}

From source file:org.wso2.carbon.uuf.internal.io.ArtifactComponentReference.java

@Override
public Stream<LayoutReference> getLayouts(Set<String> supportedExtensions) {
    Path layouts = componentDirectory.resolve(DIR_NAME_LAYOUTS);
    if (!Files.exists(layouts)) {
        return Stream.<LayoutReference>empty();
    }/*from   w  w  w.  j a va 2s  .c  o m*/
    try {
        return Files.list(layouts)
                .filter(path -> Files.isRegularFile(path) && supportedExtensions.contains(getExtension(path)))
                .map(path -> new ArtifactLayoutReference(path, this));
    } catch (IOException e) {
        throw new FileOperationException("An error occurred while listing layouts in '" + layouts + "'.", e);
    }
}

From source file:org.wso2.carbon.uuf.internal.io.ArtifactAppDeployer.java

@Override
public Set<String> deploy() {
    Stream<Path> contents;
    try {//w ww. j a  va 2  s  .  c o m
        contents = Files.list(appsRepository);
    } catch (IOException e) {
        throw new FileOperationException("Cannot list UUF apps in '" + appsRepository + "' directory.", e);
    }

    contents.filter(Files::isDirectory).forEach(appPath -> {
        Pair<String, String> appNameContextPath = getAppNameContextPath(appPath);
        pendingToDeployArtifacts.put(appNameContextPath.getRight(),
                new AppArtifact(appNameContextPath.getLeft(), appPath));
        log.debug("UUF app '{}' added to the pending deployments list.", appNameContextPath.getLeft());
    });
    return Collections.unmodifiableSet(pendingToDeployArtifacts.keySet());
}

From source file:software.coolstuff.springframework.owncloud.service.impl.local.OwncloudLocalResourceServiceCopyWebdavDirectoryTestExecutionListener.java

private void addOrCheckForEmptyDirectory(Path path) throws IOException {
    if (!Files.exists(path)) {
        Files.createDirectories(path);
    } else if (!Files.isDirectory(path)) {
        throw new IllegalStateException(
                String.format("Path %s exists but is not a Directory", path.toAbsolutePath().toString()));
    } else {/*  w  ww  .  j  a  v  a 2 s. c o m*/
        Files.list(path).forEach(p -> {
            throw new IllegalStateException(String.format("Directory %s is not empty", path));
        });
    }
}

From source file:org.ballerinalang.composer.service.workspace.local.LocalFSWorkspace.java

@Override
public JsonArray listDirectoriesInPath(String path) throws IOException {
    Path ioPath = Paths.get(path);
    JsonArray dirs = new JsonArray();
    Iterator<Path> iterator = Files.list(ioPath).iterator();
    while (iterator.hasNext()) {
        Path next = iterator.next();
        if (Files.isDirectory(next) && !Files.isHidden(next)) {
            JsonObject jsnObj = getJsonObjForFile(next, true);
            dirs.add(jsnObj);// ww  w . j a  v  a2s.  co  m
        }
    }
    return dirs;
}

From source file:org.wso2.carbon.uuf.internal.io.deployment.ArtifactAppFinder.java

private List<AppDetails> findApps(Path appsRepository) {
    try {// www .j  a  va  2 s .  co m
        return Files.list(appsRepository).filter(Files::isDirectory).map(this::getAppDetails)
                .collect(Collectors.toList());
    } catch (IOException e) {
        throw new FileOperationException("Cannot list UUF apps in '" + appsRepository + "' directory.", e);
    }
}

From source file:org.opennms.netmgt.dao.support.FilesystemResourceStorageDao.java

@Override
public Set<ResourcePath> children(ResourcePath path, int depth) {
    Preconditions.checkArgument(depth > 0, "depth must be positive");
    final File root = toFile(path);
    if (!root.isDirectory()) {
        return Collections.emptySet();
    }//www.  j a  va  2  s .co  m

    try (Stream<Path> stream = Files.list(root.toPath())) {
        return stream.filter(p -> p.toFile().isDirectory()) // filter for directories
                .filter(p -> exists(p, depth - 1)) // filter for folders with metrics
                .map(p -> ResourcePath.get(path, p.toFile().getName())).collect(Collectors.toSet());
    } catch (IOException e) {
        LOG.error("Failed to list {}. Returning empty set of children.", path, e);
        return Collections.emptySet();
    }
}

From source file:com.liferay.blade.cli.command.InstallExtensionCommandTest.java

@Test
public void testInstallCustomExtensionSubdirectory() throws Exception {
    Assume.assumeFalse(_isWindows());/* w w  w. j a  v a 2  s  .  c  om*/

    String[] args = { "extension", "install", _LINK_TO_DEPLOY_COMMAND };

    BladeTestResults bladeTestResults = TestUtil.runBlade(_rootDir, _extensionsDir, false, args);

    String output = bladeTestResults.getOutput();

    Assert.assertTrue("Expected output to contain \"successful\"\n" + output, output.contains(" successful"));

    Path rootPath = _rootDir.toPath();

    Path extensionDirPath = rootPath.resolve(Paths.get(".blade", "extensions"));

    try (Stream<Path> extensionStream = Files.list(extensionDirPath)) {

        boolean pathExists = extensionStream.map(Path::getFileName).map(Object::toString)
                .anyMatch(fileNameString -> fileNameString.startsWith("maven-profile"));

        Assert.assertTrue("maven-profile extension jar does not exist", pathExists);
    }

}

From source file:org.apache.archiva.indexer.maven.MavenIndexManagerTest.java

@Test
public void pack() throws Exception {
    createTestContext();/*from w ww.j  a  v  a2 s .c  o m*/
    Path destDir = repository.getLocalPath().resolve("org/apache/archiva/archiva-webapp/1.0");
    Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-webapp/1.0");
    org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
    mavenIndexManager.scan(ctx);
    mavenIndexManager.pack(ctx);
    assertTrue(Files.list(indexPath).filter(path -> {
        try {
            return path.getFileName().toString().endsWith(".gz") && Files.size(path) > 0;
        } catch (IOException e) {
            return false;
        }
    }).findAny().isPresent());
}