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:org.opennms.netmgt.dao.support.FilesystemResourceStorageDao.java

private boolean exists(Path root, int depth) {
    if (!root.toFile().isDirectory()) {
        return false;
    }/*  w w  w  .j  a va2s. c om*/

    try (Stream<Path> stream = Files.list(root)) {
        if (depth == 0) {
            return stream.anyMatch(isRrdFile);
        } else {
            return stream.anyMatch(p -> exists(p, depth - 1));
        }
    } catch (IOException e) {
        LOG.error("Failed to list {}. Marking path as non-existent.", root, e);
        return false;
    }
}

From source file:io.pravega.segmentstore.storage.impl.extendeds3.S3FileSystemImpl.java

@Override
public ListObjectsResult listObjects(String bucketName, String prefix) {
    ListObjectsResult result = new ListObjectsResult();
    ArrayList<S3Object> list = new ArrayList<>();
    Path path = Paths.get(this.baseDir, bucketName, prefix);
    try {//w w w  .j a va2  s. com
        if (Files.exists(path)) {
            if (Files.isDirectory(path)) {
                Files.list(path).forEach((file) -> {
                    addFileAsObjectToList(file, list, bucketName);
                });
            } else {
                addFileAsObjectToList(path, list, bucketName);
            }
        }
    } catch (IOException e) {
        throw new S3Exception("NoSuchKey", HttpStatus.SC_NOT_FOUND, "NoSuchKey", "");
    }
    result.setObjects(list);
    return result;
}

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

private JsonObject getJsonObjForFile(Path root, boolean checkChildren) {
    JsonObject rootObj = new JsonObject();
    if (null != root.getFileName()) {
        Path fileName = root.getFileName();
        if (null != fileName) {
            rootObj.addProperty("text", fileName.toString());
        }/*  w  w  w .  j  av  a  2s  .  c o  m*/
    } else {
        rootObj.addProperty("text", root.toString());
    }
    rootObj.addProperty("id", root.toAbsolutePath().toString());
    if (Files.isDirectory(root) && checkChildren) {
        rootObj.addProperty("type", "folder");
        try {
            if (Files.list(root).count() > 0) {
                rootObj.addProperty("children", Boolean.TRUE);
            } else {
                rootObj.addProperty("children", Boolean.FALSE);
            }
        } catch (IOException e) {
            logger.debug("Error while fetching children of " + root.toString(), e);
            rootObj.addProperty("error", e.toString());
        }
    } else if (Files.isRegularFile(root) && checkChildren) {
        rootObj.addProperty("type", "file");
        rootObj.addProperty("children", Boolean.FALSE);
    }
    return rootObj;
}

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

private boolean existsWithin(Path root, int depth) {
    if (depth < 0 || !root.toFile().isDirectory()) {
        return false;
    }//from  w w w .j  a va  2 s .  co m

    try (Stream<Path> stream = Files.list(root)) {
        return stream.anyMatch(p -> (isRrdFile.test(p) || existsWithin(p, depth - 1)));
    } catch (IOException e) {
        LOG.error("Failed to list {}. Marking path as non-existent.", root, e);
        return false;
    }
}

From source file:com.qwazr.server.configuration.ServerConfiguration.java

/**
 * List the configuration files// w  ww .  ja  v  a 2 s. c o  m
 *
 * @return a list with the found configuration files
 * @throws IOException if any I/O error occurs
 */
public Collection<Path> getEtcFiles() throws IOException {
    if (etcDirectories == null)
        return Collections.emptyList();
    final Set<Path> etcPaths = new LinkedHashSet<>();
    for (final Path etcDirectory : etcDirectories) {
        if (Files.exists(etcDirectory) && Files.isDirectory(etcDirectory)) {
            try (final Stream<Path> stream = Files.list(etcDirectory)) {
                stream.filter(etcFileFilter).forEach(etcPaths::add);
            }
        }
    }
    return etcPaths;
}

From source file:org.darkware.wpman.config.ReloadableWordpressConfig.java

/**
 * Load all available plugin configuration profile fragments under the given directory. No recursion is done.
 * All profile fragments must end in {@code .yml}.
 *
 * @param plugins The {@link PluginListConfig} to override with the loaded fragments.
 * @param dir The {@link Path} of the directory to scan.
 * @throws IOException If there is an error while listing the directory contents
 *///from ww  w.  j  a  v a  2s . c  o m
protected void loadPlugins(final PluginListConfig plugins, final Path dir) throws IOException {
    if (!Files.exists(dir))
        return;
    if (!Files.isDirectory(dir)) {
        WPManager.log.warn("Cannot load plugin overrides: {} is not a directory.", dir);
        return;
    }
    if (!Files.isReadable(dir)) {
        WPManager.log.warn("Cannot load plugin overrides: {} is not readable.", dir);
        return;
    }

    Files.list(dir).filter(f -> Files.isRegularFile(f)).filter(f -> f.toString().endsWith(".yml"))
            .forEach(f -> this.loadPlugin(plugins, f));
}

From source file:org.eclipse.cdt.arduino.core.internal.board.ArduinoManager.java

public void convertLibrariesDir() throws CoreException {
    Path librariesDir = ArduinoPreferences.getArduinoHome().resolve("libraries"); //$NON-NLS-1$
    if (!Files.isDirectory(librariesDir)) {
        return;/* w  ww .  j  a  v  a 2  s.  c om*/
    }

    try {
        Path tmpDir = Files.createTempDirectory("alib"); //$NON-NLS-1$
        Path tmpLibDir = tmpDir.resolve("libraries"); //$NON-NLS-1$
        Files.move(librariesDir, tmpLibDir);
        Files.list(tmpLibDir).forEach(path -> {
            try {
                Optional<Path> latest = Files.list(path)
                        .reduce((path1, path2) -> compareVersions(path1.getFileName().toString(),
                                path2.getFileName().toString()) > 0 ? path1 : path2);
                if (latest.isPresent()) {
                    Files.move(latest.get(), librariesDir.resolve(path.getFileName()));
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        recursiveDelete(tmpDir);
    } catch (RuntimeException | IOException e) {
        throw Activator.coreException(e);
    }

}

From source file:com.twosigma.beakerx.kernel.magic.command.MavenJarResolver.java

private Map<String, Path> jarsFromRepo() {
    try {//www  . j  av a2 s. c  o  m
        List<Path> collect = Files.list(Paths.get(pathToMavenRepo)).collect(Collectors.toList());
        return collect.stream().collect(Collectors.toMap(x -> x.getFileName().toString(), x -> x));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:api.wiki.WikiGenerator.java

private List<String> versionsWithDocumentation() throws IOException {
    return Files.list(docsDirectory()).map(Path::toFile).filter(File::isDirectory).map(File::getName)
            .filter(name -> !name.startsWith("_")).collect(Collectors.toList());
}

From source file:org.cirdles.webServices.calamari.PrawnFileHandlerService.java

public Path generateReportsZip(String myFileName, InputStream prawnFile, boolean useSBM, boolean userLinFits,
        String firstLetterRM) throws IOException, JAXBException, SAXException {

    String fileName = myFileName;
    if (myFileName == null) {
        fileName = DEFAULT_PRAWNFILE_NAME;
    }//from   ww  w. j  ava 2 s.c o  m

    Path uploadDirectory = Files.createTempDirectory("upload");
    Path prawnFilePathZip = uploadDirectory.resolve("prawn-file.zip");

    Files.copy(prawnFile, prawnFilePathZip);

    //file path string to extracted xml
    String extract = extract(prawnFilePathZip.toFile(), uploadDirectory.toFile());

    Path calamarirReportsFolderAlias = Files.createTempDirectory("reports-destination");
    File reportsDestinationFile = calamarirReportsFolderAlias.toFile();

    reportsEngine.setFolderToWriteCalamariReports(reportsDestinationFile);
    fileName = FilenameUtils.removeExtension(fileName);

    // this gives reportengine the name of the Prawnfile for use in report names
    prawnFileHandler.initReportsEngineWithCurrentPrawnFileName(fileName);
    prawnFileHandler.writeReportsFromPrawnFile(extract, useSBM, userLinFits, firstLetterRM);

    Files.delete(prawnFilePathZip);

    Path reportsFolder = Paths.get(reportsEngine.getFolderToWriteCalamariReportsPath()).getParent()
            .toAbsolutePath();

    Path reports = Files.list(reportsFolder).findFirst().orElseThrow(() -> new IllegalStateException());

    Path reportsZip = zip(reports);
    recursiveDelete(reports);

    return reportsZip;
}