Example usage for java.nio.file Files walk

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

Introduction

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

Prototype

public static Stream<Path> walk(Path start, FileVisitOption... options) throws IOException 

Source Link

Document

Return a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file.

Usage

From source file:org.wildfly.swarm.proc.Monitor.java

private Optional<ArchivedResult> getPreviousResults(Path currentOutput, File dir) throws IOException {
    try (Stream<Path> stream = Files.walk(dir.toPath(), 1)) {
        return stream.filter(path -> Files.isRegularFile(path))
                .filter(path -> path.getFileName().toString().endsWith(".csv"))
                .filter(path -> !isSameFile(currentOutput, path)).map(path -> {
                    String fileName = path.getFileName().toString();
                    Version version = Version.valueOf(fileName.substring(0, fileName.lastIndexOf(".")));
                    return new ArchivedResult(version, path.toFile());
                }).sorted(Comparator.comparing(ArchivedResult::getVersion).reversed()).findFirst();
    }//from w  w  w .java  2s  .  co  m
}

From source file:org.eclipse.jdt.ls.core.internal.JDTUtilsTest.java

@Test
public void testFakeCompilationUnit() throws Exception {
    String tempDir = System.getProperty("java.io.tmpdir");
    File dir = new File(tempDir, "/test/src/org/eclipse");
    dir.mkdirs();/*from www .ja  v  a 2s . c om*/
    File file = new File(dir, "Test.java");
    file.createNewFile();
    URI uri = file.toURI();
    JDTUtils.resolveCompilationUnit(uri);
    IProject project = WorkspaceHelper.getProject(ProjectsManager.DEFAULT_PROJECT_NAME);
    IFile iFile = project.getFile("/src/org/eclipse/Test.java");
    assertTrue(iFile.getFullPath().toString() + " doesn't exist.", iFile.exists());
    Path path = Paths.get(tempDir + "/test");
    Files.walk(path, FileVisitOption.FOLLOW_LINKS).sorted(Comparator.reverseOrder()).map(Path::toFile)
            .forEach(File::delete);
}

From source file:dk.dma.dmiweather.service.FTPLoader.java

private static void deleteRecursively(File lastTempDir) throws IOException {
    Path rootPath = lastTempDir.toPath();
    //noinspection ResultOfMethodCallIgnored
    Files.walk(rootPath, FileVisitOption.FOLLOW_LINKS).sorted(Comparator.reverseOrder()) // flips the tree so leefs are deleted first
            .map(Path::toFile).peek(f -> log.debug("deleting file " + f.getAbsolutePath()))
            .forEach(File::delete);

}

From source file:org.codice.ddf.commands.catalog.IngestCommand.java

@Override
protected Object executeWithSubject() throws Exception {

    final CatalogFacade catalog = getCatalog();
    final File inputFile = new File(filePath);

    if (!inputFile.exists()) {
        printErrorMessage("File or directory [" + filePath + "] must exist.");
        console.println("If the file does indeed exist, try putting the path in quotes.");
        return null;
    }/*  w w w  . java2s .c o m*/

    if (deprecatedBatchSize != DEFAULT_BATCH_SIZE) {
        // user specified the old style batch size, so use that
        printErrorMessage(
                "Batch size positional argument is DEPRECATED, please use --batchsize option instead.");
        batchSize = deprecatedBatchSize;
    }

    if (batchSize <= 0) {
        printErrorMessage(
                "A batch size of [" + batchSize + "] was supplied. Batch size must be greater than 0.");
        return null;
    }

    if (!StringUtils.isEmpty(failedDir)) {
        failedIngestDirectory = new File(failedDir);
        if (!verifyFailedIngestDirectory()) {
            return null;
        }

        /**
         * Batch size is always set to 1, when using an Ingest Failure Directory.  If a batch size is specified by the user, issue 
         * a warning stating that a batch size of 1 will be used.
         */
        if (batchSize != DEFAULT_BATCH_SIZE) {
            console.println("WARNING: An ingest failure directory was supplied in addition to a batch size of "
                    + batchSize
                    + ". When using an ingest failure directory, the batch size must be 1. Setting batch size to 1.");
        }

        batchSize = 1;
    }

    BundleContext bundleContext = getBundleContext();
    if (!DEFAULT_TRANSFORMER_ID.equals(transformerId)) {
        ServiceReference[] refs = null;

        try {
            refs = bundleContext.getServiceReferences(InputTransformer.class.getName(),
                    "(|" + "(" + Constants.SERVICE_ID + "=" + transformerId + ")" + ")");
        } catch (InvalidSyntaxException e) {
            throw new IllegalArgumentException("Invalid transformer transformerId: " + transformerId, e);
        }

        if (refs == null || refs.length == 0) {
            throw new IllegalArgumentException("Transformer " + transformerId + " not found");
        } else {
            transformer = (InputTransformer) bundleContext.getService(refs[0]);
        }
    }

    Stream<Path> ingestStream = Files.walk(inputFile.toPath(), FileVisitOption.FOLLOW_LINKS);

    int totalFiles = (inputFile.isDirectory()) ? inputFile.list().length : 1;
    fileCount.getAndSet(totalFiles);

    final ArrayBlockingQueue<Metacard> metacardQueue = new ArrayBlockingQueue<>(batchSize * multithreaded);

    ExecutorService queueExecutor = Executors.newSingleThreadExecutor();

    final long start = System.currentTimeMillis();

    printProgressAndFlush(start, fileCount.get(), 0);

    queueExecutor.submit(() -> buildQueue(ingestStream, metacardQueue, start));

    final ScheduledExecutorService batchScheduler = Executors.newSingleThreadScheduledExecutor();

    BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(multithreaded);
    RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
    ExecutorService executorService = new ThreadPoolExecutor(multithreaded, multithreaded, 0L,
            TimeUnit.MILLISECONDS, blockingQueue, rejectedExecutionHandler);

    submitToCatalog(batchScheduler, executorService, metacardQueue, catalog, start);

    while (!doneBuildingQueue.get() || processingThreads.get() != 0) {
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            LOGGER.error("Ingest 'Waiting for processing to finish' thread interrupted: {}", e);
        }
    }

    try {
        queueExecutor.shutdown();
        executorService.shutdown();
        batchScheduler.shutdown();
    } catch (SecurityException e) {
        LOGGER.error("Executor service shutdown was not permitted: {}", e);
    }

    printProgressAndFlush(start, fileCount.get(), ingestCount.get() + ignoreCount.get());
    long end = System.currentTimeMillis();
    console.println();
    String elapsedTime = timeFormatter.print(new Period(start, end).withMillis(0));

    console.println();
    console.printf(" %d file(s) ingested in %s %n", ingestCount.get(), elapsedTime);

    LOGGER.info("{} file(s) ingested in {} [{} records/sec]", ingestCount.get(), elapsedTime,
            calculateRecordsPerSecond(ingestCount.get(), start, end));
    INGEST_LOGGER.info("{} file(s) ingested in {} [{} records/sec]", ingestCount.get(), elapsedTime,
            calculateRecordsPerSecond(ingestCount.get(), start, end));

    if (fileCount.get() != ingestCount.get()) {
        console.println();
        if ((fileCount.get() - ingestCount.get() - ignoreCount.get()) >= 1) {
            String failedAmount = Integer.toString(fileCount.get() - ingestCount.get() - ignoreCount.get());
            printErrorMessage(
                    failedAmount + " file(s) failed to be ingested.  See the ingest log for more details.");
            INGEST_LOGGER.warn("{} files(s) failed to be ingested.", failedAmount);
        }
        if (ignoreList != null) {
            String ignoredAmount = Integer.toString(ignoreCount.get());
            printColor(Ansi.Color.YELLOW,
                    ignoredAmount + " file(s) ignored.  See the ingest log for more details.");
            INGEST_LOGGER.warn("{} files(s) were ignored.", ignoredAmount);
        }
    }
    console.println();

    return null;
}

From source file:io.kahu.hawaii.util.call.sql.DbRequestBuilderRepository.java

private void walkDirectory(final String directory) {
    try {/*  w ww  .  j a  v  a2 s.  c  om*/
        URI uri = this.getClass().getResource(directory).toURI();
        Path myPath;
        if (uri.getScheme().equals("jar")) {
            FileSystem fileSystem = null;
            try {
                fileSystem = FileSystems.getFileSystem(uri);
            } catch (Exception e) {
                // ignore
            }
            if (fileSystem == null) {
                fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap());
            }
            myPath = fileSystem.getPath(directory);
        } else {
            myPath = Paths.get(uri);
        }
        Stream<Path> walk = Files.walk(myPath, 1);
        walk.forEach((path) -> {
            String name = path.getFileName().toString();
            if (name.endsWith(".sql")) {
                DbRequestPrototype p = createPrototype(directory, name);
                if (p != null) {
                    try {
                        add(new DbRequestBuilder(p));
                    } catch (ServerException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.roda.core.common.monitor.TransferredResourcesScanner.java

public CloseableIterable<OptionalWithCause<LiteRODAObject>> listTransferredResources() {
    CloseableIterable<OptionalWithCause<LiteRODAObject>> resources = null;

    try {//ww w .  j  ava  2s.  c  o  m
        final Stream<Path> files = Files.walk(basePath, FileVisitOption.FOLLOW_LINKS)
                .filter(path -> !path.equals(basePath));
        final Iterator<Path> fileIterator = files.iterator();

        resources = new CloseableIterable<OptionalWithCause<LiteRODAObject>>() {
            @Override
            public void close() throws IOException {
                files.close();
            }

            @Override
            public Iterator<OptionalWithCause<LiteRODAObject>> iterator() {

                return new Iterator<OptionalWithCause<LiteRODAObject>>() {
                    @Override
                    public boolean hasNext() {
                        return fileIterator.hasNext();
                    }

                    @Override
                    public OptionalWithCause<LiteRODAObject> next() {
                        Path file = fileIterator.next();
                        Optional<LiteRODAObject> liteResource = LiteRODAObjectFactory
                                .get(TransferredResource.class, Arrays.asList(file.toString()), false);
                        return OptionalWithCause.of(liteResource);
                    }
                };
            }
        };
    } catch (IOException e) {
        LOGGER.error("Errored when file walking to list transferred resources");
    }

    return resources;
}

From source file:com.depas.utils.FileUtils.java

/**
 * This will give all the entries (files and directories) found in the given directory
 * path and based on the predicate given.
 * To return just files add file.isFile() to the predicate passed in
 * ex. f -> f.getName().toLowerCase().contains("myfile") && f.isFile()
 * If a file is given then just that file is return in the list.
 * @param dir//from  w w  w .j  a  v  a 2s  .c  o  m
 * @param predicate
 * @return List<File> - list of entries (files/dir) found in the given directory
 * @throws IOException
 */
public static List<File> getAllEntriesInDir(Path dir, Predicate<File> predicate) throws IOException {
    if (dir == null) {
        throw new IllegalArgumentException("The Path arugment needs to be set.");
    }

    if (predicate == null) {
        throw new IllegalArgumentException("The Predicate arugment needs to be set.");
    }
    try (Stream<Path> stream = Files.walk(dir, Integer.MAX_VALUE)) {
        List<File> files = stream.map(p -> p.toFile()).filter(predicate).sorted().collect(Collectors.toList());

        return files;
    }
}

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

public static CloseableIterable<Resource> recursivelyListPath(final Path basePath, final Path path)
        throws NotFoundException, GenericException {
    CloseableIterable<Resource> resourceIterable;
    try {/*  ww  w  .j a  va  2s. c  o m*/
        final Stream<Path> walk = Files.walk(path, FileVisitOption.FOLLOW_LINKS);
        final Iterator<Path> pathIterator = walk.iterator();

        // skip root
        if (pathIterator.hasNext()) {
            pathIterator.next();
        }

        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 {
                walk.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:org.artifactory.support.core.bundle.AbstractSupportBundleService.java

/**
 * Lists previously created bundles//from  w w  w  .j  a  va2  s. c  om
 *
 * @return archive/s
 */
@Override
public final List<String> list() {

    List<String> archives = Lists.newLinkedList();

    try {
        Files.walk(getOutputDirectory().toPath(), FileVisitOption.FOLLOW_LINKS).filter(Files::isRegularFile)
                .filter(f -> f.toFile().getName().startsWith(SUPPORT_BUNDLE_PREFIX)).filter(p -> FilenameUtils
                        .getExtension(p.toString()).equals(CompressionService.ARCHIVE_EXTENSION))
                .sorted(new Comparator<Path>() {
                    @Override
                    public int compare(Path o1, Path o2) {
                        return o2.toString().compareTo(o1.toString());
                    }
                }).forEach(p -> archives.add(p.toFile().getName()));
    } catch (IOException e) {
        e.printStackTrace();
    }

    return archives;
}

From source file:com.gnadenheimer.mg.utils.Utils.java

/**
 * Delete AutoBackUps if older than 60 days
 *//*from w ww.  j a  va2 s .co  m*/
public void deleteOldBackUps() {
    try {
        Path dir = Paths.get(getPersistenceMap().get("backUpDir")); // specify your directory

        Optional<Path> lastFilePath = Files.list(dir) // here we get the stream with full directory listing
                .filter(f -> Files.isDirectory(f)) // exclude files from listing
                .min(Comparator.comparingLong(f -> f.toFile().lastModified())); // finally get the last file using simple comparator by lastModified field

        if (lastFilePath.isPresent()) // your folder may be empty
        {
            FileTime fileTime = Files.getLastModifiedTime(lastFilePath.get());
            Long age = DAYS.between(LocalDateTime.ofInstant(fileTime.toInstant(), ZoneOffset.UTC),
                    LocalDateTime.now());
            if (age > 30) {
                Files.walk(lastFilePath.get(), FileVisitOption.FOLLOW_LINKS).sorted(Comparator.reverseOrder())
                        .map(Path::toFile).peek(System.out::println).forEach(File::delete);
                deleteOldBackUps();
            }
        }
    } catch (Exception ex) {
        LOGGER.error(Thread.currentThread().getStackTrace()[1].getMethodName(), ex);
        JOptionPane.showMessageDialog(null,
                Thread.currentThread().getStackTrace()[1].getMethodName() + " - " + ex.getMessage());
    }
}