Example usage for java.nio.file Files walkFileTree

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

Introduction

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

Prototype

public static Path walkFileTree(Path start, FileVisitor<? super Path> visitor) throws IOException 

Source Link

Document

Walks a file tree.

Usage

From source file:io.liveoak.testtools.AbstractTestCase.java

@AfterClass
public static void tearDownMongo() throws IOException {
    if (mongoLauncher != null) {
        mongoLauncher.stopMongo();//from www.  j ava  2  s . c  o  m

        // wait for it to stop
        long start = System.currentTimeMillis();
        while (mongoLauncher.serverRunning(mongoHost, mongoPort, (e) -> {
            if (System.currentTimeMillis() - start > 120000)
                throw new RuntimeException(e);
        })) {

            if (System.currentTimeMillis() - start > 120000) {
                throw new RuntimeException("mongod process still seems to be running (2m timeout)");
            }
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                throw new RuntimeException("Interrupted!");
            }
        }

        // now delete the data dir except log file
        Files.walkFileTree(new File(mongoLauncher.getDbPath()).toPath(), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (!file.startsWith(mongoLauncher.getLogPath())) {
                    Files.delete(file);
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                try {
                    Files.delete(dir);
                } catch (DirectoryNotEmptyException ignored) {
                }
                return FileVisitResult.CONTINUE;
            }
        });

        mongoLauncher = null;
    }
}

From source file:io.stallion.dataAccess.file.FilePersisterBase.java

@Override
public List<T> fetchAll() {
    File target = new File(Settings.instance().getTargetFolder());
    if (!target.isDirectory()) {
        if (getItemController().isWritable()) {
            target.mkdirs();/*w w  w.  j a  v  a  2s  . c  om*/
        } else {
            throw new ConfigException(String.format(
                    "The JSON bucket %s (path %s) is read-only, but does not exist in the file system. Either create the folder, make it writable, or remove it from the configuration.",
                    getItemController().getBucket(), getBucketFolderPath()));
        }
    }
    TreeVisitor visitor = new TreeVisitor();
    Path folderPath = FileSystems.getDefault().getPath(getBucketFolderPath());
    try {
        Files.walkFileTree(folderPath, visitor);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    List<T> objects = new ArrayList<>();
    for (Path path : visitor.getPaths()) {
        if (!matchesExtension(path.toString())) {
            continue;
        }

        if (path.toString().contains(".#")) {
            continue;
        }
        if (path.getFileName().startsWith(".")) {
            continue;
        }
        T o = fetchOne(path.toString());
        if (o != null) {
            objects.add(o);
        }

    }
    objects.sort(new PropertyComparator<T>(sortField));
    if (sortDirection.toLowerCase().equals("desc")) {
        Collections.reverse(objects);
    }

    return objects;
}

From source file:com.cloudbees.clickstack.util.Files2.java

public static void chmodReadOnly(@Nonnull Path path) throws RuntimeIOException {

    SimpleFileVisitor<Path> setReadOnlyFileVisitor = new SimpleFileVisitor<Path>() {
        @Override//www.j  a  v a 2  s .  c  om
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (Files.isDirectory(file)) {
                throw new IllegalStateException("no dir expected here");
            } else {
                Files.setPosixFilePermissions(file, PERMISSION_R);
            }
            return super.visitFile(file, attrs);
        }

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            Files.setPosixFilePermissions(dir, PERMISSION_RX);
            return super.preVisitDirectory(dir, attrs);
        }
    };
    try {
        Files.walkFileTree(path, setReadOnlyFileVisitor);
    } catch (IOException e) {
        throw new RuntimeIOException("Exception changing permissions to readonly for " + path, e);
    }
}

From source file:com.drunkendev.io.recurse.tests.RecursionTest.java

/**
 * Answer provided by yawn./*from  w w w . j a  va  2 s.c  o m*/
 *
 * This method uses a {@link FileVisitor} implementation that counts files
 * and directories.
 *
 * This test uses NIO {@link Files#walkFileTree(Path, FileVisitor)}.
 *
 * @see     <a href="http://stackoverflow.com/a/2056352/140037">Stack-Overflow answer by yawn</a>
 */
//    @Test
public void testWalkFileTree() {
    System.out.println("\nTEST: Walk File Tree");
    time(() -> {
        PathCounterFileVisitor counter = new PathCounterFileVisitor();
        try {
            Files.walkFileTree(startPath, counter);
        } catch (IOException ex) {
            fail(ex.getMessage());
        }
        System.out.format("Files: %d, dirs: %d. ", counter.getFiles(), counter.getDirs());
    });
}

From source file:org.apdplat.superword.tools.PdfParser.java

public static void parseZip(String zipFile) {
    long start = System.currentTimeMillis();
    LOGGER.info("?ZIP" + zipFile);
    try (FileSystem fs = FileSystems.newFileSystem(Paths.get(zipFile), WordClassifier.class.getClassLoader())) {
        for (Path path : fs.getRootDirectories()) {
            LOGGER.info("?" + path);
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

                @Override//from   ww w  . ja  v a2 s. c o m
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    LOGGER.info("?" + file);
                    // ?
                    Path temp = Paths.get("target/it-software-domain-temp.pdf");
                    Files.copy(file, temp, StandardCopyOption.REPLACE_EXISTING);
                    parseFile(temp.toFile().getAbsolutePath());
                    return FileVisitResult.CONTINUE;
                }

            });
        }
    } catch (Exception e) {
        LOGGER.error("?", e);
    }
    long cost = System.currentTimeMillis() - start;
    LOGGER.info("?" + cost + "");
}

From source file:org.sleuthkit.autopsy.experimental.autoingest.SingleUserCaseImporter.java

/**
 * This causes iteration over all .aut files in the baseCaseInput path,
 * calling SingleUserCaseConverter.importCase() for each one.
 */// w ww.  j  ava2s  . c  om
public void importCases() throws Exception {
    openLog(baseCaseOutput.toFile());
    log(NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.StartingBatch")
            + baseCaseInput.toString() + " "
            + NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.to") + " "
            + baseCaseOutput.toString()); //NON-NLS

    // iterate for .aut files
    FindDotAutFolders dotAutFolders = new FindDotAutFolders();
    try {
        Path walked = Files.walkFileTree(baseCaseInput, dotAutFolders);
    } catch (IOException ex) {
        log(NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.ErrorFindingAutFiles")
                + " " + ex.getMessage()); //NON-NLS
    }

    ArrayList<ImportCaseData> ableToProcess = new ArrayList<>();
    ArrayList<ImportCaseData> unableToProcess = new ArrayList<>();

    SingleUserCaseConverter scc = new SingleUserCaseConverter();

    // validate we can convert the .aut file, one by one
    for (FoundAutFile f : dotAutFolders.getCandidateList()) {
        this.oldCaseName = f.getPath().getFileName().toString();

        // Test image output folder for uniqueness, find a unique folder for it if we can
        File specificOutputFolder = baseImageOutput.resolve(oldCaseName).toFile();
        String newImageName = oldCaseName;
        if (specificOutputFolder.exists()) {
            // Not unique. add numbers before timestamp to specific image output name
            String timeStamp = TimeStampUtils.getTimeStampOnly(oldCaseName);
            newImageName = TimeStampUtils.removeTimeStamp(oldCaseName);
            int number = 1;
            String temp = ""; //NON-NLS
            while (specificOutputFolder.exists()) {
                if (number == Integer.MAX_VALUE) {
                    // It never became unique, so give up.
                    throw new Exception(NbBundle.getMessage(SingleUserCaseImporter.class,
                            "SingleUserCaseImporter.NonUniqueOutputFolder") + newImageName); //NON-NLS
                }
                temp = newImageName + "_" + Integer.toString(number) + timeStamp; //NON-NLS
                specificOutputFolder = baseImageOutput.resolve(temp).toFile();
                ++number;
            }
            newImageName = temp;
        }
        Path imageOutput = baseImageOutput.resolve(newImageName);
        imageOutput.toFile().mkdirs(); // Create image output folder

        // Test case output folder for uniqueness, find a unique folder for it if we can
        specificOutputFolder = baseCaseOutput.resolve(oldCaseName).toFile();
        newCaseName = oldCaseName;
        if (specificOutputFolder.exists()) {
            // not unique. add numbers before timestamp to specific case output name
            String timeStamp = TimeStampUtils.getTimeStampOnly(oldCaseName); //NON-NLS
            newCaseName = TimeStampUtils.removeTimeStamp(oldCaseName);
            int number = 1;
            String temp = ""; //NON-NLS
            while (specificOutputFolder.exists()) {
                if (number == Integer.MAX_VALUE) {
                    // It never became unique, so give up.
                    throw new Exception(NbBundle.getMessage(SingleUserCaseImporter.class,
                            "SingleUserCaseImporter.NonUniqueOutputFolder") + newCaseName); //NON-NLS
                }
                temp = newCaseName + "_" + Integer.toString(number) + timeStamp; //NON-NLS
                specificOutputFolder = baseCaseOutput.resolve(temp).toFile();
                ++number;
            }
            newCaseName = temp;
        }
        Path caseOutput = baseCaseOutput.resolve(newCaseName);
        caseOutput.toFile().mkdirs(); // Create case output folder

        /**
         * Test if the input path has a corresponding image input folder and
         * no repeated case names in the path. If both of these conditions
         * are true, we can process this case, otherwise not.
         */
        // Check that there is an image folder if they are trying to copy it
        boolean canProcess = true;
        Path imageInput = null;
        String relativeCaseName = TimeStampUtils
                .removeTimeStamp(baseCaseInput.relativize(f.getPath()).toString());
        Path testImageInputsFromOldCase = Paths.get(baseImageInput.toString(), relativeCaseName);
        if (copyImages) {
            if (!testImageInputsFromOldCase.toFile().isDirectory()) {
                // Mark that we are unable to process this item
                canProcess = false;
            } else {
                imageInput = testImageInputsFromOldCase;
            }
            if (imageInput == null) {
                throw new Exception(NbBundle.getMessage(SingleUserCaseImporter.class,
                        "SingleUserCaseImporter.SourceImageMissing") + " " + f.getPath()); //NON-NLS
            }

            // If case name is in the image path, it causes bad things to happen with the parsing. Test for this.
            for (int x = 0; x < imageInput.getNameCount(); ++x) {
                if (oldCaseName.toLowerCase().equals(imageInput.getName(x).toString().toLowerCase())) {
                    // Mark that we are unable to process this item
                    canProcess = false;
                }
            }
        } else {
            imageInput = testImageInputsFromOldCase;
        }

        // Create an Import Case Data object for this case
        SingleUserCaseConverter.ImportCaseData icd = scc.new ImportCaseData(imageInput, f.getPath(),
                imageOutput, caseOutput, oldCaseName, newCaseName, f.getAutFile().toString(),
                f.getFolderName().toString(), copyImages, deleteCase);

        if (canProcess) {
            ableToProcess.add(icd);
        } else {
            unableToProcess.add(icd);
        }
    }

    // Create text to be populated in the confirmation dialog
    StringBuilder casesThatWillBeProcessed = new StringBuilder();
    StringBuilder casesThatWillNotBeProcessed = new StringBuilder();

    casesThatWillBeProcessed
            .append(NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.WillImport"))
            .append(SEP); // NON-NLS
    if (ableToProcess.isEmpty()) {
        casesThatWillBeProcessed
                .append(NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.None"))
                .append(SEP); // NON-NLS
    } else {
        for (ImportCaseData i : ableToProcess) {
            casesThatWillBeProcessed.append(i.getCaseInputFolder().toString()).append(SEP);
        }
    }

    if (!unableToProcess.isEmpty()) {
        casesThatWillNotBeProcessed.append(
                NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.WillNotImport"))
                .append(SEP); // NON-NLS
        for (ImportCaseData i : unableToProcess) {
            casesThatWillNotBeProcessed.append(i.getCaseInputFolder().toString()).append(SEP);
        }
    }

    JTextArea jta = new JTextArea(
            casesThatWillBeProcessed.toString() + SEP + casesThatWillNotBeProcessed.toString());
    jta.setEditable(false);
    JScrollPane jsp = new JScrollPane(jta) {
        private static final long serialVersionUID = 1L;

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(700, 480);
        }
    };

    // Show confirmation dialog
    SwingUtilities.invokeLater(() -> {
        userAnswer = JOptionPane.showConfirmDialog(WindowManager.getDefault().getMainWindow(), jsp,
                NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.ContinueWithImport"), // NON-NLS
                OK_CANCEL_OPTION);
        synchronized (threadWaitNotifyLock) {
            threadWaitNotifyLock.notify();
        }
    });

    // Wait while the user handles the confirmation dialog
    synchronized (threadWaitNotifyLock) {
        try {
            threadWaitNotifyLock.wait();
        } catch (InterruptedException ex) {
            Logger.getLogger(SingleUserCaseImporter.class.getName()).log(Level.SEVERE, "Threading Issue", ex); //NON-NLS
            throw new Exception(ex);
        }
    }

    // If the user wants to proceed, do so.
    if (userAnswer == JOptionPane.OK_OPTION) {
        boolean result = true; // if anything went wrong, result becomes false.
        // Feed .aut files in one by one for processing
        for (ImportCaseData i : ableToProcess) {
            try {
                log(NbBundle.getMessage(SingleUserCaseImporter.class,
                        "SingleUserCaseImporter.StartedProcessing") + i.getCaseInputFolder() + " "
                        + NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.to") + " "
                        + i.getCaseOutputFolder()); //NON-NLS
                SingleUserCaseConverter.importCase(i);
                handleAutoIngestLog(i);
                log(NbBundle.getMessage(SingleUserCaseImporter.class,
                        "SingleUserCaseImporter.FinishedProcessing") + i.getCaseInputFolder() + " "
                        + NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.to") + " "
                        + i.getCaseOutputFolder()); //NON-NLS

            } catch (Exception ex) {
                log(NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.FailedToComplete")
                        + i.getCaseInputFolder() + " "
                        + NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.to") + " "
                        + i.getCaseOutputFolder() + " " + ex.getMessage()); //NON-NLS
                result = false;
            }
        }

        log(NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.CompletedBatch")
                + baseCaseInput.toString() + " "
                + NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.to") + " "
                + baseCaseOutput.toString()); //NON-NLS

        closeLog();
        if (notifyOnComplete != null) {
            notifyOnComplete.importDoneCallback(result, ""); // NON-NLS
        }
    } else {
        // The user clicked cancel. Abort.
        log(NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.AbortingBatch")
                + baseCaseInput.toString() + " "
                + NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.to") + " "
                + baseCaseOutput.toString()); //NON-NLS

        closeLog();
        if (notifyOnComplete != null) {
            notifyOnComplete.importDoneCallback(false,
                    NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.Cancelled")); // NON-NLS
        }
    }
}

From source file:gov.vha.isaac.rf2.filter.RF2Filter.java

@Override
public void execute() throws MojoExecutionException {
    if (!inputDirectory.exists() || !inputDirectory.isDirectory()) {
        throw new MojoExecutionException("Path doesn't exist or isn't a folder: " + inputDirectory);
    }/* ww  w  .  j av  a 2s  .co  m*/

    if (module == null) {
        throw new MojoExecutionException("You must provide a module or namespace for filtering");
    }

    moduleStrings_.add(module + "");

    outputDirectory.mkdirs();
    File temp = new File(outputDirectory, inputDirectory.getName());
    temp.mkdirs();

    Path source = inputDirectory.toPath();
    Path target = temp.toPath();
    try {
        getLog().info("Reading from " + inputDirectory.getAbsolutePath());
        getLog().info("Writing to " + outputDirectory.getCanonicalPath());

        summary_.append("This content was filtered by an RF2 filter tool.  The parameters were module: "
                + module + " software version: " + converterVersion);
        summary_.append("\r\n\r\n");

        getLog().info("Checking for nested child modules");

        //look in sct2_Relationship_ files, find anything where the 6th column (destinationId) is the 
        //starting module ID concept - and add that sourceId (5th column) to our list of modules to extract
        Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.toFile().getName().startsWith("sct2_Relationship_")) {
                    //don't look for quotes, the data is bad, and has floating instances of '"' all by itself
                    CSVReader csvReader = new CSVReader(
                            new InputStreamReader(new FileInputStream(file.toFile())), '\t',
                            CSVParser.NULL_CHARACTER);
                    String[] line = csvReader.readNext();
                    if (!line[4].equals("sourceId") || !line[5].equals("destinationId")) {
                        csvReader.close();
                        throw new IOException("Unexpected error looking for nested modules");
                    }
                    line = csvReader.readNext();
                    while (line != null) {
                        if (line[5].equals(moduleStrings_.get(0))) {
                            moduleStrings_.add(line[4]);
                        }
                        line = csvReader.readNext();
                    }
                    csvReader.close();
                }
                return FileVisitResult.CONTINUE;
            }
        });

        log("Full module list (including detected nested modules: "
                + Arrays.toString(moduleStrings_.toArray(new String[moduleStrings_.size()])));

        Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                Path targetdir = target.resolve(source.relativize(dir));
                try {
                    //this just creates the sub-directory in the target
                    Files.copy(dir, targetdir);
                } catch (FileAlreadyExistsException e) {
                    if (!Files.isDirectory(targetdir))
                        throw e;
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                handleFile(file, target.resolve(source.relativize(file)));
                return FileVisitResult.CONTINUE;
            }
        });

        Files.write(new File(temp, "FilterInfo.txt").toPath(), summary_.toString().getBytes(),
                StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    } catch (IOException e) {
        throw new MojoExecutionException("Failure", e);
    }

    getLog().info("Filter Complete");

}

From source file:org.apdplat.superword.tools.WordClassifierForYouDao.java

public static void parseZip(String zipFile) {
    LOGGER.info("?ZIP" + zipFile);
    try (FileSystem fs = FileSystems.newFileSystem(Paths.get(zipFile),
            WordClassifierForYouDao.class.getClassLoader())) {
        for (Path path : fs.getRootDirectories()) {
            LOGGER.info("?" + path);
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

                @Override//from  ww  w.  j a v a  2  s  . c o  m
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    LOGGER.info("?" + file);
                    // ?
                    Path temp = Paths.get("target/origin-html-temp.txt");
                    Files.copy(file, temp, StandardCopyOption.REPLACE_EXISTING);
                    parseFile(temp.toFile().getAbsolutePath());
                    return FileVisitResult.CONTINUE;
                }

            });
        }
    } catch (Exception e) {
        LOGGER.error("?", e);
    }
}

From source file:org.zaproxy.VerifyScripts.java

private static void readFiles() throws Exception {
    Optional<String> path = Arrays.stream(System.getProperty("java.class.path").split(File.pathSeparator))
            .filter(e -> e.endsWith("/scripts")).findFirst();
    assertThat(path).as("The scripts directory was not found on the classpath.").isPresent();

    List<Path> unexpectedFiles = new ArrayList<>();
    MutableInt depth = new MutableInt();

    files = new ArrayList<>();
    Files.walkFileTree(Paths.get(path.get()), new SimpleFileVisitor<Path>() {

        @Override//from w  ww . j a  va2s  .  c  o  m
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            depth.increment();
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            if (depth.intValue() != SCRIPT_TYPE_DIR_DEPTH) {
                unexpectedFiles.add(file);
                return FileVisitResult.CONTINUE;
            }

            if (!isExpectedNonScriptFile(file)) {
                files.add(file);
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            depth.decrement();
            return FileVisitResult.CONTINUE;
        }
    });

    assertThat(unexpectedFiles).as("Files found not in a script type directory.").isEmpty();

    Collections.sort(files);
}

From source file:fr.duminy.jbackup.core.archive.FileCollector.java

private long collect(final List<SourceWithPath> collectedFiles, final Path source,
        final IOFileFilter directoryFilter, final IOFileFilter fileFilter, final Cancellable cancellable)
        throws IOException {
    final long[] totalSize = { 0L };

    SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
        @Override/*ww w . j  av  a2s . co  m*/
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            super.preVisitDirectory(dir, attrs);
            if ((directoryFilter == null) || source.equals(dir) || directoryFilter.accept(dir.toFile())) {
                return CONTINUE;
            } else {
                return SKIP_SUBTREE;
            }
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if ((cancellable != null) && cancellable.isCancelled()) {
                return TERMINATE;
            }

            super.visitFile(file, attrs);

            if (!Files.isSymbolicLink(file)) {
                if ((fileFilter == null) || fileFilter.accept(file.toFile())) {
                    LOG.trace("visitFile {}", file.toAbsolutePath());
                    collectedFiles.add(new SourceWithPath(source, file));
                    totalSize[0] += Files.size(file);
                }
            }

            return CONTINUE;
        }
    };
    Files.walkFileTree(source, visitor);

    return totalSize[0];
}