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.codice.ddf.admin.application.service.command.ProfileListCommand.java

private void listProfileFiles() {
    try (Stream<Path> files = Files.list(profilePath)) {
        files.filter(Files::isRegularFile)
                .filter(file -> file.toAbsolutePath().toString().endsWith(PROFILE_EXTENSION))
                .forEach(profile -> console.println(FilenameUtils.removeExtension(profile.toFile().getName())));
    } catch (IOException e) {
        printError("Error occurred when locating profiles");
        LOGGER.error("An error occurred when locating profiles", e);
    }/* w ww .j a  v  a  2s . c  o  m*/
}

From source file:org.openehr.designer.repository.file.FileTemplateRepository.java

@PostConstruct
public void init() throws IOException {
    Files.createDirectories(repositoryLocation);

    Files.list(repositoryLocation)
            .filter(path -> path.getFileName().toString().endsWith(".adlt") && !Files.isDirectory(path))
            .forEach(path -> {// w w w  .j a v a  2  s.com
                try {

                    List<Archetype> archetypes = TemplateDeserializer.deserialize(Files.newInputStream(path));
                    templateMap.put(archetypes.get(0).getArchetypeId().getValue(), archetypes);

                } catch (Exception e) {
                    LOG.error("Error parsing template {}. Will be ignored", path.getFileName(), e);
                }
            });
}

From source file:aiai.ai.station.ArtifactCleanerAtStation.java

public void fixedDelay() {
    if (!globals.isStationEnabled || !currentExecState.isInit) {
        // don't delete anything until station will receive the list of actual flow instances
        return;/*from  ww  w  .  j a v  a2s . co m*/
    }

    for (StationTask task : stationTaskService.findAll()) {
        if (currentExecState.isState(task.flowInstanceId, Enums.FlowInstanceExecState.DOESNT_EXIST)) {
            log.info("Delete obsolete task with id {}", task.getTaskId());
            stationTaskService.deleteById(task.getTaskId());
        }
    }
    synchronized (StationSyncHolder.stationGlobalSync) {
        try {
            final BooleanHolder isEmpty = new BooleanHolder(true);
            Files.list(globals.stationTaskDir.toPath()).forEach(s -> {
                isEmpty.value = true;
                try {
                    Files.list(s).forEach(t -> {
                        isEmpty.value = false;
                        try {
                            File taskYaml = new File(t.toFile(), Consts.TASK_YAML);
                            if (!taskYaml.exists()) {
                                FileUtils.deleteDirectory(t.toFile());
                                // IDK is that bug or side-effect. so delete one more time
                                FileUtils.deleteDirectory(t.toFile());
                            }
                        } catch (IOException e) {
                            log.error("#090.01 Error delete path " + t, e);
                        }
                    });
                } catch (IOException e) {
                    log.error("#090.04 Error while cleaning up broken tasks", e);
                }
                if (isEmpty.value) {
                    FileUtils.deleteQuietly(s.toFile());
                }
            });
        } catch (IOException e) {
            log.error("#090.07 Error while cleaning up broken tasks", e);
        }
    }
}

From source file:com.thoughtworks.go.agent.common.util.JarUtilTest.java

@Test
public void shouldExtractJars() throws Exception {
    File sourceFile = new File(PATH_WITH_HASHES + "test-agent.jar");
    File outputTmpDir = temporaryFolder.newFolder();
    Set<File> files = new HashSet<>(JarUtil.extractFilesInLibDirAndReturnFiles(sourceFile,
            jarEntry -> jarEntry.getName().endsWith(".class"), outputTmpDir));

    Set<File> actualFiles = Files.list(outputTmpDir.toPath()).map(Path::toFile).collect(Collectors.toSet());

    assertEquals(files, actualFiles);/*from  w ww. ja v  a2 s.  co m*/
    assertEquals(files.size(), 2);
    Set<String> fileNames = files.stream().map(File::getName).collect(Collectors.toSet());
    assertEquals(fileNames,
            new HashSet<>(Arrays.asList("ArgPrintingMain.class", "HelloWorldStreamWriter.class")));
}

From source file:aiai.ai.station.StationTaskService.java

@PostConstruct
public void postConstruct() {
    if (globals.isUnitTesting) {
        return;/* w w  w. ja  v  a 2  s .c  o m*/
    }
    if (!globals.stationTaskDir.exists()) {
        return;
    }
    try {
        Files.list(globals.stationTaskDir.toPath()).forEach(p -> {
            final File topDir = p.toFile();
            if (!topDir.isDirectory()) {
                return;
            }
            try {
                Files.list(topDir.toPath()).forEach(s -> {
                    long taskId = Long.parseLong(s.toFile().getName());
                    File taskYamlFile = new File(s.toFile(), Consts.TASK_YAML);
                    if (taskYamlFile.exists()) {
                        try (FileInputStream fis = new FileInputStream(taskYamlFile)) {
                            StationTask task = StationTaskUtils.to(fis);
                            map.put(taskId, task);
                        } catch (IOException e) {
                            log.error("Error #3", e);
                            throw new RuntimeException("Error #3", e);
                        }
                    }
                });
            } catch (IOException e) {
                log.error("Error #2", e);
                throw new RuntimeException("Error #2", e);
            }
        });
    } catch (IOException e) {
        log.error("Error #1", e);
        throw new RuntimeException("Error #1", e);
    }
    //noinspection unused
    int i = 0;
}

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

private Path zip(Path target) throws IOException {
    Path zipFilePath = target.resolveSibling("reports.zip");

    try (FileSystem zipFileFileSystem = FileSystems.newFileSystem(URI.create("jar:" + zipFilePath.toUri()),
            ZIP_FILE_ENV)) {//from w  w  w.  jav  a2s .  c  om

        Files.list(target).forEach(entry -> {
            try {
                Files.copy(entry, zipFileFileSystem.getPath("/" + entry.getFileName()));
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });
    }

    return zipFilePath;
}

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

private JsonArray getJsonArrayForDirs(List<Path> rootDirs) {
    JsonArray rootArray = new JsonArray();
    for (Path root : rootDirs) {
        JsonObject rootObj = getJsonObjForFile(root, false);
        try {/* www .j  a  v a  2 s .c  o m*/
            if (Files.isDirectory(root) && Files.list(root).count() > 0) {
                JsonArray children = listFilesInPath(root.toFile().getAbsolutePath(), Arrays.asList(BAL_EXT));
                rootObj.add("children", children);
            }
        } catch (IOException e) {
            logger.debug("Error while traversing children of " + e.toString(), e);
            rootObj.addProperty("error", e.toString());
        }
        if (Files.isDirectory(root)) {
            rootArray.add(rootObj);
        }
    }
    return rootArray;
}

From source file:org.silverpeas.core.util.logging.LogsAccessor.java

/**
 * Gets the name of the logs in used in Silverpeas.
 * @return a set of Silverpeas logs.//from   ww w .  j ava  2  s  . c  o m
 * @throws IOException if an error occurs while accessing the logs.
 */
public Set<String> getAllLogs() throws IOException {
    String logPath = SystemWrapper.get().getProperty(SILVERPEAS_LOG_DIR);
    try (final Stream<Path> paths = Files.list(Paths.get(logPath))) {
        return paths.filter(path -> "log".equalsIgnoreCase(FilenameUtils.getExtension(path.toString())))
                .map(path -> path.getFileName().toString()).collect(Collectors.toSet());
    }
}

From source file:org.springframework.cloud.gcp.storage.integration.inbound.GcsInboundFileSynchronizerTests.java

@After
@Before//  ww  w.ja v  a2s .  com
public void cleanUp() throws IOException {
    Path testDirectory = Paths.get("test");

    if (Files.exists(testDirectory)) {
        if (Files.isDirectory(testDirectory)) {
            try (Stream<Path> files = Files.list(testDirectory)) {
                files.forEach((path) -> {
                    try {
                        Files.delete(path);
                    } catch (IOException ioe) {
                        LOGGER.info("Error deleting test file.", ioe);
                    }
                });
            }
        }

        Files.delete(testDirectory);
    }
}

From source file:ru.xxlabaza.popa.command.CommandBuildContent.java

@Override
@SneakyThrows//from w  ww  .  j  av  a2  s .c o  m
public void execute(String[] args) {
    val content = folder.getContent();
    val assets = folder.getAssets();

    val build = folder.getBuild();
    if (!Files.exists(build)) {
        log.info("Creating folder '{}'", build);
        Files.createDirectories(build);
    }

    Files.walk(content).filter(it -> CONTENT_FILE_PATH_MATCHER.matches(it.getFileName())).forEach(it -> {
        Path outputPath = build.resolve(content.relativize(it));
        log.info("Processing '{}'", it);
        String result = templaterFacade.process(it);
        log.info("Writing result to '{}'", outputPath);
        FileSystemUtils.write(outputPath, result);
    });

    log.info("Copying '{}' content to '{}' folder", assets, build);
    Files.list(assets).forEach(it -> {
        copy(it, build.resolve(it.getFileName()));
    });
}