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

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

Introduction

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

Prototype

public static Collection listFiles(File directory, String[] extensions, boolean recursive) 

Source Link

Document

Finds files within a given directory (and optionally its subdirectories) which match an array of extensions.

Usage

From source file:musicmetadatak1009705.Main.java

public static void aVAJAVAFileMp3Scan(File dir) {
    try {//ww  w .ja v  a2s . c  om
        String[] extensions = new String[] { "mp3" };
        System.out.println(
                "Getting all .mp3 files in " + dir.getCanonicalPath() + " including those in subdirectories");
        List<File> files = (List<File>) FileUtils.listFiles(dir, extensions, true);
        for (File file : files) {
            System.out.println("file: " + file.getCanonicalPath());
        }
    } catch (IOException ex) {
    }
}

From source file:m3umaker.exFiles.java

public List getFiles(String Mdir) {
    List<File> files0 = null;
    try {//from  www .  j  a  va2 s .co m
        File dir = new File(Mdir);
        mDIR = dir.getCanonicalPath();
        String[] extensions = new String[] { "mp3", "wma", "flac", "aac", "amr", "m4a", "m4r", "ogg", "wav",
                "wavepack", "wma", "flac", "aac", "amr", "m4a", "m4r", "ogg", "wav", "wavepack", "avi", "flv",
                "mp4", "wmv", "3gp", "rmvb", "mkv", "mpg", "vob", "mov" };
        files0 = (List<File>) FileUtils.listFiles(dir, extensions, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return files0;
}

From source file:grnet.filter.XMLSource.java

public Collection<File> getXMLs() {

    String[] extensions = { "xml" };

    Collection<File> files = FileUtils.listFiles(getSource(), extensions, true);

    return files;

}

From source file:net.rptools.tokentool.AppSetup.java

public static void install(String versionString) {
    System.setProperty("appHome", getAppHome("logs").getAbsolutePath());
    log = LogManager.getLogger(AppSetup.class);

    File overlayVer = new File(getAppHome().getAbsolutePath() + "/version.txt");
    Collection<File> existingOverLays = FileUtils.listFiles(AppConstants.OVERLAY_DIR,
            ImageUtil.SUPPORTED_FILE_FILTER, TrueFileFilter.INSTANCE);
    log.info("Overlays installed: " + existingOverLays.size());

    // Only install overlays once or if version.text is missing or version is newer
    // Overlays are stored in a version packaged structure so we can later install only newer overlays if wanted
    String installedVersion = "0";
    try {/* w w  w . j  a  va 2 s .  com*/
        if (overlayVer.exists()) {
            installedVersion = FileUtils.readFileToString(overlayVer, Charset.defaultCharset());
        } else {
            FileUtils.writeStringToFile(overlayVer, versionString, Charset.defaultCharset());
        }
    } catch (IOException ioe) {
        log.error(ioe);
    }

    if (existingOverLays.isEmpty() || isNewerVersion(TokenTool.getVersion(), installedVersion)) {
        try {
            installDefaultOverlays();
        } catch (IOException e) {
            log.error(e);
        }

        // Update version file to new version
        try {
            FileUtils.writeStringToFile(overlayVer, versionString, Charset.defaultCharset());
        } catch (IOException e) {
            log.error(e);
        }
    }
}

From source file:minij.TestFiles.java

private static List<Object[]> getFiles(Class<? extends Exception> exceptionClass, Path... paths) {
    List<Object[]> files = new LinkedList<>();

    for (Path path : paths) {
        for (File file : FileUtils.listFiles(path.toFile(), null, true)) {
            files.add(new Object[] { file, exceptionClass });
        }/*ww w  .j  a  v  a2  s  .  c o m*/
    }

    return files;
}

From source file:de.tudarmstadt.ukp.experiments.dip.wp1.documents.Step9AgreementCollector.java

@SuppressWarnings("unchecked")
public static void computeObservedAgreement(File goldDataFolder, File outputDir) throws Exception {
    // iterate over query containers
    for (File f : FileUtils.listFiles(goldDataFolder, new String[] { "xml" }, false)) {
        QueryResultContainer queryResultContainer = QueryResultContainer
                .fromXML(FileUtils.readFileToString(f, "utf-8"));

        for (QueryResultContainer.SingleRankedResult rankedResult : queryResultContainer.rankedResults) {

            // only non-empty and annotated results
            // No annotations found for document: clueWebID: clueweb12-1407wb-22-10643, queryID: 1006
            // <clueWebID>clueweb12-1407wb-22-10643</clueWebID>
            // <score>5.93809186</score>
            // <additionalInfo>indri</additionalInfo>
            // <plainText></plainText>

            if (rankedResult.plainText != null && !rankedResult.plainText.isEmpty()) {
                if (rankedResult.mTurkRelevanceVotes.isEmpty()) {
                    //                        throw new IllegalStateException("No annotations found for document: "
                    System.err.println("No annotations found for document: " + "clueWebID: "
                            + rankedResult.clueWebID + ", queryID: " + queryResultContainer.qID);
                } else {

                    // first, get all the sentence IDs
                    byte[] bytes = new BASE64Decoder()
                            .decodeBuffer(new ByteArrayInputStream(rankedResult.originalXmi.getBytes()));

                    JCas jCas = JCasFactory.createJCas();
                    XmiCasDeserializer.deserialize(new ByteArrayInputStream(bytes), jCas.getCas());

                    // for each sentence, we'll collect all its annotations
                    TreeMap<Integer, SortedMap<String, String>> sentencesAndRelevanceAnnotations = collectSentenceIDs(
                            jCas);/*w w  w .  ja v a 2  s  .  co m*/

                    // now we will the map with mturk annotations
                    // the list of true/false for each sentence will be consistent (the annotator ordering remains)
                    for (QueryResultContainer.MTurkRelevanceVote mTurkRelevanceVote : rankedResult.mTurkRelevanceVotes) {
                        for (QueryResultContainer.SingleSentenceRelevanceVote sentenceRelevanceVote : mTurkRelevanceVote.singleSentenceRelevanceVotes) {

                            String sentenceIDString = sentenceRelevanceVote.sentenceID;
                            if (sentenceIDString == null || sentenceIDString.isEmpty()) {
                                throw new IllegalStateException("Empty sentence ID for turker "
                                        + mTurkRelevanceVote.turkID + ", HIT: " + mTurkRelevanceVote.hitID
                                        + ", clueWebID: " + rankedResult.clueWebID + ", queryID: "
                                        + queryResultContainer.qID);
                            } else {

                                Integer sentenceIDInt = Integer.valueOf(sentenceIDString);
                                String value = sentenceRelevanceVote.relevant;

                                // add to the list

                                // sanity check first
                                if (sentencesAndRelevanceAnnotations.get(sentenceIDInt)
                                        .containsKey(mTurkRelevanceVote.turkID)) {
                                    System.err.println("Annotations for sentence " + sentenceIDInt
                                            + " for turker " + mTurkRelevanceVote.turkID + " are duplicate");
                                }

                                sentencesAndRelevanceAnnotations.get(sentenceIDInt)
                                        .put(mTurkRelevanceVote.turkID, value);
                            }
                        }
                    }

                    //                    for (Map.Entry<Integer, SortedMap<String, String>> entry : sentencesAndRelevanceAnnotations
                    //                            .entrySet()) {
                    //                        System.out.println(entry.getKey() + ": " + entry.getValue());
                    //                    }

                    // we collect only the "clean" ones
                    Map<Integer, SortedMap<String, String>> cleanSentencesAndRelevanceAnnotations = new HashMap<>();

                    // sanity check -- all sentences are covered with the same number of annotations
                    for (Map.Entry<Integer, SortedMap<String, String>> entry : sentencesAndRelevanceAnnotations
                            .entrySet()) {
                        SortedMap<String, String> singleSentenceAnnotations = entry.getValue();

                        // remove empty sentences
                        if (singleSentenceAnnotations.values().isEmpty()) {
                            //                                throw new IllegalStateException(
                            System.err.println("Empty annotations for sentence, " + "sentenceID: "
                                    + entry.getKey() + ", " + "clueWebID: " + rankedResult.clueWebID
                                    + ", queryID: " + queryResultContainer.qID + "; number of assignments: "
                                    + singleSentenceAnnotations.values().size() + ", expected: "
                                    + NUMBER_OF_TURKERS_PER_HIT + ". Sentence will be skipped in evaluation");
                        } else if (singleSentenceAnnotations.values().size() != NUMBER_OF_TURKERS_PER_HIT) {
                            System.err.println("Inconsistent annotations for sentences, " + "sentenceID: "
                                    + entry.getKey() + ", " + "clueWebID: " + rankedResult.clueWebID
                                    + ", queryID: " + queryResultContainer.qID + "; number of assignments: "
                                    + singleSentenceAnnotations.values().size() + ", expected: "
                                    + NUMBER_OF_TURKERS_PER_HIT + ". Sentence will be skipped in evaluation");
                        } else {
                            cleanSentencesAndRelevanceAnnotations.put(entry.getKey(), entry.getValue());
                        }
                    }

                    // fill the annotation study

                    CodingAnnotationStudy study = new CodingAnnotationStudy(NUMBER_OF_TURKERS_PER_HIT);
                    study.addCategory("true");
                    study.addCategory("false");

                    for (SortedMap<String, String> singleSentenceAnnotations : cleanSentencesAndRelevanceAnnotations
                            .values()) {
                        // only non-empty sentences
                        Collection<String> values = singleSentenceAnnotations.values();
                        if (!values.isEmpty() && values.size() == NUMBER_OF_TURKERS_PER_HIT) {
                            study.addItemAsArray(values.toArray());
                        }

                    }

                    //                    System.out.println(study.getCategories());

                    // Fleiss' multi-pi.
                    FleissKappaAgreement fleissKappaAgreement = new FleissKappaAgreement(study);

                    double percentage;
                    try {
                        percentage = fleissKappaAgreement.calculateObservedAgreement();
                    } catch (InsufficientDataException ex) {
                        // dkpro-statistics feature, see https://github.com/dkpro/dkpro-statistics/issues/24
                        percentage = 1.0;
                    }

                    if (!Double.isNaN(percentage)) {
                        rankedResult.observedAgreement = percentage;
                        //                        System.out.println(sentencesAndRelevanceAnnotations.values());
                    } else {
                        System.err.println("Observed agreement is NaN.");
                    }
                }
            }
        }

        // and save the query to output dir
        File outputFile = new File(outputDir, queryResultContainer.qID + ".xml");
        FileUtils.writeStringToFile(outputFile, queryResultContainer.toXML(), "utf-8");
        System.out.println("Finished " + outputFile);
    }
}

From source file:massbank.svn.MSDBUpdateUtil.java

/**
 *
 *//*from  w ww.  j  a  v a 2  s  . c  o  m*/
public static boolean updateFiles(File srcDir, File destDir, String[] extensions) {
    boolean isUpdated = false;
    if (!srcDir.exists()) {
        return false;
    }
    if (!destDir.exists()) {
        destDir.mkdirs();
    }
    try {
        List<File> srcFileList = (List<File>) FileUtils.listFiles(srcDir, extensions, false);
        List<File> destFileList = (List<File>) FileUtils.listFiles(destDir, extensions, false);
        FileDifference diff = new FileDifference(srcFileList, destFileList);
        String[] addFilePaths = diff.getAddFilePaths();
        String[] delFileNames = diff.getDeleteFileNames();
        if (addFilePaths.length > 0) {
            isUpdated = true;
            for (String path : addFilePaths) {
                FileUtils.copyFileToDirectory(new File(path), destDir);
            }
        }
        if (delFileNames.length > 0) {
            isUpdated = true;
            for (String name : delFileNames) {
                String path = destDir.getPath() + File.separator + name;
                FileUtils.deleteQuietly(new File(path));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return isUpdated;
}

From source file:com.ebay.logstorm.server.utils.LogStormServerDebug.java

private static File findAssemblyJarFile() {
    String projectRootDir = System.getProperty("user.dir");
    String assemblyModuleTargeDirPath = projectRootDir + "/assembly/target/";
    File assemblyTargeDirFile = new File(assemblyModuleTargeDirPath);
    if (!assemblyTargeDirFile.exists()) {
        throw new IllegalStateException(
                assemblyModuleTargeDirPath + " not found, please execute 'mvn install -DskipTests' under "
                        + projectRootDir + " to build the project firstly and retry");
    }//ww w. j  a v  a  2s .  co  m
    String jarFileNameWildCard = "logstorm-assembly-*.jar";
    Collection<File> jarFiles = FileUtils.listFiles(assemblyTargeDirFile,
            new WildcardFileFilter(jarFileNameWildCard), TrueFileFilter.INSTANCE);
    if (jarFiles.size() == 0) {
        throw new IllegalStateException(
                "jar is not found, please execute 'mvn install -DskipTests' from project root firstly and retry");
    }
    File jarFile = jarFiles.iterator().next();
    LOG.debug("Found pipeline.jar: {}", jarFile.getAbsolutePath());
    return jarFile;
}

From source file:com.doplgangr.secrecy.FileSystem.storage.java

public static void DeleteRecursive(java.io.File directory) {
    try {/*  ww  w .  jav  a 2  s  . c  o  m*/
        Collection files = FileUtils.listFiles(directory, null, true);
        for (Object file : files)
            purgeFile((File) file);
    } catch (Exception e) {
        Util.log(e);
    } finally {
        try {
            FileUtils.cleanDirectory(directory);
        } catch (Exception ignored) {
        }
    }
}

From source file:exec.examples.IoHelper.java

public static List<String> findAllZips(String dir) {
    List<String> zips = Lists.newLinkedList();
    for (File f : FileUtils.listFiles(new File(dir), new String[] { "zip" }, true)) {
        zips.add(f.getAbsolutePath());// w  w w.  jav a  2 s. c  om
    }
    return zips;
}