Example usage for org.apache.commons.io FileUtils iterateFiles

List of usage examples for org.apache.commons.io FileUtils iterateFiles

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils iterateFiles.

Prototype

public static Iterator iterateFiles(File directory, String[] extensions, boolean recursive) 

Source Link

Document

Allows iteration over the files in a given directory (and optionally its subdirectories) which match an array of extensions.

Usage

From source file:com.yifanlu.PSXperiaTool.ZpakCreate.java

public void create(boolean noCompress) throws IOException {
    Logger.info("Generating zpak file from directory %s with compression = %b", mDirectory.getPath(),
            !noCompress);/*from   ww  w .  j a  v  a2 s.  c om*/
    IOFileFilter filter = new IOFileFilter() {
        public boolean accept(File file) {
            if (file.getName().startsWith(".")) {
                Logger.debug("Skipping file %s", file.getPath());
                return false;
            }
            return true;
        }

        public boolean accept(File file, String s) {
            if (s.startsWith(".")) {
                Logger.debug("Skipping file %s", file.getPath());
                return false;
            }
            return true;
        }
    };
    Iterator<File> it = FileUtils.iterateFiles(mDirectory, filter, TrueFileFilter.INSTANCE);
    ZipOutputStream out = new ZipOutputStream(mOut);
    out.setMethod(noCompress ? ZipEntry.STORED : ZipEntry.DEFLATED);
    while (it.hasNext()) {
        File current = it.next();
        FileInputStream in = new FileInputStream(current);
        ZipEntry zEntry = new ZipEntry(
                current.getPath().replace(mDirectory.getPath(), "").substring(1).replace("\\", "/"));
        if (noCompress) {
            zEntry.setSize(in.getChannel().size());
            zEntry.setCompressedSize(in.getChannel().size());
            zEntry.setCrc(getCRC32(current));
        }
        out.putNextEntry(zEntry);
        Logger.verbose("Adding file %s", current.getPath());
        int n;
        while ((n = in.read(mBuffer)) != -1) {
            out.write(mBuffer, 0, n);
        }
        in.close();
        out.closeEntry();
    }
    out.close();
    Logger.debug("Done with ZPAK creation.");
}

From source file:net.ripe.rpki.commons.validation.interop.BBNConformanceTest.java

@Test
public void shouldParseAllObjects() throws IOException {
    int objectCount = 0;
    int errorCount = 0;
    int exceptionCount = 0;

    Iterator<File> fileIterator = FileUtils.iterateFiles(new File(PATH_TO_BBN_OBJECTS),
            new String[] { "cer", "crl", "mft", "roa" }, true);
    while (fileIterator.hasNext()) {
        objectCount++;/*from   w w  w . j a v a 2 s .  c o m*/
        File file = fileIterator.next();
        byte[] encoded = Files.toByteArray(file);
        ValidationResult result = ValidationResult.withLocation(file.getName());

        try {
            if (file.getName().endsWith("cer")) {
                new X509ResourceCertificateParser().parse(result, encoded);
            } else if (file.getName().endsWith("crl")) {
                X509Crl.parseDerEncoded(encoded, result);
            } else if (file.getName().endsWith("mft")) {
                new ManifestCmsParser().parse(result, encoded);
            } else if (file.getName().endsWith("roa")) {
                new RoaCmsParser().parse(result, encoded);
            }

            if (result.hasFailures() && file.getName().startsWith("good")) {
                System.err.println("Supposed to be good: " + file.getName());
                errorCount++;
            } else if (!result.hasFailures() && file.getName().startsWith("bad")) {
                System.err.println("Supposed to be bad: " + file.getName());
                errorCount++;
            } else {
                System.out.println(file.getName() + " -> " + result.hasFailures());
            }
        } catch (RuntimeException ex) {
            System.err.println("Exception while parsing " + file.getName());
            exceptionCount++;
        }
    }

    System.out.println(objectCount + " objects: " + errorCount + " errors, " + exceptionCount + " exceptions");
}

From source file:info.magnolia.ui.framework.command.CleanTempFilesCommand.java

@Override
public boolean execute(final Context context) throws Exception {
    String tmpDirPath = configurationProperties.getProperty("magnolia.upload.tmpdir");
    File tmpDir = new File(tmpDirPath);
    Date date = DateUtils.addHours(new Date(), TIME_OFFSET_IN_HOURS); // current time minus 12 hours
    Iterator<File> files = FileUtils.iterateFiles(tmpDir, FileFilterUtils.ageFileFilter(date),
            FileFilterUtils.ageFileFilter(date));
    while (files.hasNext()) {
        files.next().delete();//  w  w w. j a  v a  2s  .c  o  m
    }
    return true;
}

From source file:com.oxiane.maven.xqueryMerger.Merger.java

@Override
public void execute() throws MojoExecutionException {
    Path sourceRootPath = inputSources.toPath();
    Path destinationRootPath = outputDirectory.toPath();
    IOFileFilter filter = buildFilter();
    Iterator<File> it = FileUtils.iterateFiles(inputSources, filter, FileFilterUtils.directoryFileFilter());
    if (!it.hasNext()) {
        getLog().warn("No file found matching " + filter.toString() + " in " + inputSources.getAbsolutePath());
    }/*www .j  a v  a2 s. c  o m*/
    while (it.hasNext()) {
        File sourceFile = it.next();
        Path fileSourcePath = sourceFile.toPath();
        Path relativePath = sourceRootPath.relativize(fileSourcePath);
        getLog().debug("[Merger] found source: " + fileSourcePath.toString());
        getLog().debug("[Merger]    relative path is " + relativePath.toString());
        StreamSource source = new StreamSource(sourceFile);
        XQueryMerger merger = new XQueryMerger(source);
        merger.setMainQuery();
        File destinationFile = destinationRootPath.resolve(relativePath).toFile();
        getLog().debug("[Merger]    destination will be " + destinationFile.getAbsolutePath());
        try {
            String result = merger.merge();
            destinationFile.getParentFile().mkdirs();
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(destinationFile),
                    merger.getEncoding());
            osw.write(result);
            osw.flush();
            osw.close();
            getLog().debug("[Merger] " + relativePath.toString() + " merged into "
                    + destinationFile.getAbsolutePath());
        } catch (ParsingException ex) {
            getLog().error(ex.getMessage());
            throw new MojoExecutionException("Merge of " + sourceFile.getAbsolutePath() + " fails", ex);
        } catch (FileNotFoundException ex) {
            getLog().error(ex.getMessage());
            throw new MojoExecutionException(
                    "Unable to create destination " + destinationFile.getAbsolutePath(), ex);
        } catch (IOException ex) {
            getLog().error(ex.getMessage());
            throw new MojoExecutionException("While writing " + destinationFile.getAbsolutePath(), ex);
        }
    }
}

From source file:com.chingo247.structureapi.plan.io.StructurePlanReader.java

public List<IStructurePlan> readDirectory(File structurePlanDirectory, boolean printstuff, ForkJoinPool pool) {
    Iterator<File> fit = FileUtils.iterateFiles(structurePlanDirectory, new String[] { "xml" }, true);
    SchematicManager sdm = SchematicManager.getInstance();
    sdm.load(structurePlanDirectory);//w  ww. jav  a 2 s  .  com

    Map<String, StructurePlanProcessor> processors = new HashMap<>();

    while (fit.hasNext()) {
        File structurePlanFile = fit.next();
        StructurePlanProcessor spp = new StructurePlanProcessor(structurePlanFile);
        processors.put(structurePlanFile.getAbsolutePath(), spp);
        pool.execute(spp);
    }

    List<IStructurePlan> plans = new ArrayList<>();
    try {

        for (StructurePlanProcessor spp : processors.values()) {
            IStructurePlan plan = spp.get();
            if (plan != null) {
                plans.add(plan);
            }
        }
    } catch (Exception ex) {
        java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,
                ex.getMessage(), ex);
    }

    return plans;
}

From source file:com.eincs.decanter.render.soy.SoyAcre.java

/**
 * //from w  w  w  .  j av a2  s. co m
 * @param directory
 * @return
 */
public static SoyAcre forDirectory(final File directory) {
    final String[] EXTENTIONS = new String[] { SOY_FILE_EXTENTION };
    return new SoyAcre() {
        @Override
        public void addToFileSet(SoyFileSet.Builder fileSetBuilder) {
            Set<File> soyFiles = Sets.newHashSet(FileUtils.iterateFiles(directory, EXTENTIONS, true));
            for (File soyFile : soyFiles) {
                fileSetBuilder.add(soyFile);
            }
        }
    };
}

From source file:GitHelper.java

public static ArrayList<String> getRepositoryTests(File repositoryPath, JSONObject repository)
        throws IOException, JSONException {
    String testPath = repository.getString("path");
    File fullPath = new File(repositoryPath.getAbsolutePath() + "/" + testPath);
    JSONArray arr = repository.getJSONArray("fileTypes");
    String[] fileTypes = new String[arr.length()];
    for (int i = 0; i < arr.length(); i++) {
        fileTypes[i] = (arr.getString(i));
    }/*from   w ww  .j  a  v a  2  s.c om*/
    Iterator iter = FileUtils.iterateFiles(fullPath, fileTypes, true);
    ArrayList<String> tests = new ArrayList<>();

    while (iter.hasNext()) {
        File file = (File) iter.next();
        if (fileHasBadLines(file, repository)) {
            continue;
        }
        String test = file.getName();
        if (test != null) {
            tests.add(test);
        }
    }
    return tests;
}

From source file:edu.ku.brc.util.HelpIndexer.java

/**
 * //w  ww  . j  a v  a 2 s  . c  o  m
 */
@SuppressWarnings("unchecked")
public void indexIt() {
    if (!jhm.exists()) {
        System.out.println("jhm file not found.");
        return;
    }
    if (!topDir.isDirectory()) {
        System.out.println("directory does not exist.");
        return;
    }
    try {
        mapLines = FileUtils.readLines(jhm);
    } catch (IOException ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(HelpIndexer.class, ex);
        System.out.println("Error reading map file: " + ex.getMessage());
        return;
    }

    Vector<String> lines = new Vector<String>();
    String[] ext = { "html" };
    Iterator<File> helpFiles = FileUtils.iterateFiles(topDir, ext, true);
    while (helpFiles.hasNext()) {
        File file = helpFiles.next();
        System.out.println("Processing " + file.getName());
        processFile(file, lines);
    }

    System.out.println();
    System.out.println("all done.");

    File outFile = new File(outFileName);
    try {

        //add lines to top of file
        lines.insertElementAt("<?xml version='1.0' encoding='ISO-8859-1'  ?>", 0);
        lines.insertElementAt("<!DOCTYPE index", 1);
        lines.insertElementAt("  PUBLIC \"-//Sun Microsystems Inc.//DTD JavaHelp Index Version 1.0//EN\"", 2);
        lines.insertElementAt("         \"http://java.sun.com/products/javahelp/index_1_0.dtd\">", 3);
        lines.insertElementAt("<index version=\"1.0\">", 4);
        lines.insertElementAt(" ", 5);

        //and bottom...
        lines.add(" ");
        lines.add("</index>");

        FileUtils.writeLines(outFile, lines);
    } catch (IOException ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(HelpIndexer.class, ex);
        System.out.println("error writing output file: " + ex.getMessage());
    }
}

From source file:com.alibaba.citrus.maven.eclipse.base.eclipse.RemoveCacheMojo.java

/**
 * A list of all the not available marker <code>File</code>s in the localRepository. If there are no marker files
 * then an empty list is returned.//from   w w  w.ja va 2  s .  c om
 *
 * @return all the not available marker files in the localRepository or an empty list.
 */
private List/* <File> */ getNotAvailableMarkerFiles() {
    File localRepositoryBaseDirectory = new File(localRepository.getBasedir());
    List markerFiles = new ArrayList();

    Iterator iterator = FileUtils.iterateFiles(localRepositoryBaseDirectory,
            new SuffixFileFilter(IdeUtils.NOT_AVAILABLE_MARKER_FILE_SUFFIX), TrueFileFilter.INSTANCE);
    while (iterator.hasNext()) {
        File notAvailableMarkerFile = (File) iterator.next();
        markerFiles.add(notAvailableMarkerFile);
    }
    return markerFiles;
}

From source file:gobblin.example.simplejson.SimpleJsonSource.java

@Override
public List<WorkUnit> getWorkunits(SourceState state) {
    List<WorkUnit> workUnits = Lists.newArrayList();

    if (!state.contains(ConfigurationKeys.SOURCE_FILEBASED_FILES_TO_PULL)) {
        return workUnits;
    }//from  w  ww.j  a  v a2 s.  c om

    // Create a single snapshot-type extract for all files
    Extract extract = new Extract(state, Extract.TableType.SNAPSHOT_ONLY,
            state.getProp(ConfigurationKeys.EXTRACT_NAMESPACE_NAME_KEY, "ExampleNamespace"), "ExampleTable");

    String filesToPull = state.getProp(ConfigurationKeys.SOURCE_FILEBASED_FILES_TO_PULL);
    File tempFileDir = new File("test_temp/"); // TODO: Delete the dir after completion.
    tempFileDir.mkdir();
    String tempFileDirAbsolute = "";
    try {
        tempFileDirAbsolute = tempFileDir.getCanonicalPath(); // Retrieve absolute path of temp folder
    } catch (IOException e) {
        e.printStackTrace();
    }

    int nameCount = 0;
    int csvCount = 0;
    for (String file : Splitter.on(',').omitEmptyStrings().split(filesToPull)) {
        Iterator it = FileUtils.iterateFiles(new File(file), null, true);
        while (it.hasNext()) {
            try {
                File newFile = (File) it.next();
                String basePath = newFile.getCanonicalPath(); // Retrieve absolute path of source
                Path path = newFile.toPath();

                //call to rest api:, provide with file basePath
                String extension = "";
                System.out.println("basePath is" + basePath);
                int i = basePath.lastIndexOf('.');
                System.out.println("i");
                if (i > 0) {
                    extension = basePath.substring(i + 1);
                }

                String url_file_name = "";
                int j = basePath.lastIndexOf('/');
                if (j > 0) {
                    url_file_name = basePath.substring(j + 1);
                }

                //hand off to rest api
                if (extension.equals("csv")) {
                    System.out.println("CSVCSVCSV");
                    csvCount += 1;
                    System.out.println("CURL________________________________________");
                    //Include basePath, filename, location you want to store file
                    System.out.println(
                            "curl http://localhost:8080" + basePath + "/" + Integer.toString(nameCount));
                    //10.0.2.2 is localhost from vagrant
                    // Insert the nameCount so that it can be joined back later.
                    Process p = Runtime.getRuntime()
                            .exec("curl http://localhost:8080" + basePath + "/" + Integer.toString(nameCount));
                    // String myUrl = "http://localhost:8080/parse" + basePath + "&" + url_file_name + "&" + tempFileDirAbsolute;
                    // System.out.println("------------------------------");
                    // System.out.println(myUrl);
                    // try {
                    //   URL url = new URL(myUrl);
                    //   HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    //   connection.setRequestMethod("GET");
                    //   connection.connect();
                    // } catch (Exception e) {
                    //   e.printStackTrace();
                    // }

                }
                // Print filename and associated metadata 
                System.out.println(basePath);
                BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
                // System.out.println("  creationTime: " + attr.creationTime());
                // System.out.println("  lastAccessTime: " + attr.lastAccessTime());
                // System.out.println("  lastModifiedTime: " + attr.lastModifiedTime());
                // System.out.println("  isDirectory: " + attr.isDirectory());
                // System.out.println("  isOther: " + attr.isOther());
                // System.out.println("  isRegularFile: " + attr.isRegularFile());
                // System.out.println("  isSymbolicLink: " + attr.isSymbolicLink());
                // System.out.println("  size: " + attr.size()); 
                // System.out.println(" ");

                //creating intermediate JSON
                JSONObject intermediate = new JSONObject();
                intermediate.put("filename", basePath);
                intermediate.put("timestamp", String.valueOf((new Date()).getTime()));
                intermediate.put("namespace", getMacAddress());

                intermediate.put("creationTime", String.valueOf(attr.creationTime()));
                intermediate.put("lastAccessTime", String.valueOf(attr.lastAccessTime()));
                intermediate.put("lastModifiedTime", String.valueOf(attr.lastModifiedTime()));
                intermediate.put("isDirectory", String.valueOf(attr.isDirectory()));
                intermediate.put("isOther", String.valueOf(attr.isOther()));
                intermediate.put("isRegularFile", String.valueOf(attr.isRegularFile()));
                intermediate.put("isSymbolicLink", String.valueOf(attr.isSymbolicLink()));
                intermediate.put("size", attr.size());

                // Create intermediate temp file
                nameCount += 1;
                String intermediateName = "/generated" + String.valueOf(nameCount) + ".json";
                String finalName = tempFileDirAbsolute + intermediateName;
                FileWriter generated = new FileWriter(finalName);
                generated.write(intermediate.toJSONString());
                generated.flush();
                generated.close();

                // Create one work unit for each file to pull
                WorkUnit workUnit = new WorkUnit(state, extract);
                workUnit.setProp(SOURCE_FILE_KEY, finalName);
                workUnits.add(workUnit);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // write out number of files found to temp file
        try {
            FileWriter numCsvFiles = new FileWriter(tempFileDirAbsolute + "/numCsvFiles.txt");
            numCsvFiles.write("" + csvCount);
            numCsvFiles.flush();
            numCsvFiles.close();

            FileWriter numFiles = new FileWriter(tempFileDirAbsolute + "/numFiles.txt");
            numFiles.write("" + nameCount);
            numFiles.flush();
            numFiles.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    return workUnits;
}