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:org.datavec.audio.recordreader.BaseAudioRecordReader.java

@Override
public void initialize(InputSplit split) throws IOException, InterruptedException {
    inputSplit = split;/*from   ww  w.ja  v  a2  s.  com*/
    if (split instanceof BaseInputSplit) {
        URI[] locations = split.locations();
        if (locations != null && locations.length >= 1) {
            if (locations.length > 1) {
                List<File> allFiles = new ArrayList<>();
                for (URI location : locations) {
                    File iter = new File(location);
                    if (iter.isDirectory()) {
                        Iterator<File> allFiles2 = FileUtils.iterateFiles(iter, null, true);
                        while (allFiles2.hasNext())
                            allFiles.add(allFiles2.next());
                    }

                    else
                        allFiles.add(iter);
                }

                iter = allFiles.iterator();
            } else {
                File curr = new File(locations[0]);
                if (curr.isDirectory())
                    iter = FileUtils.iterateFiles(curr, null, true);
                else
                    iter = Collections.singletonList(curr).iterator();
            }
        }
    }

    else if (split instanceof InputStreamInputSplit) {
        record = new ArrayList<>();
        InputStreamInputSplit split2 = (InputStreamInputSplit) split;
        InputStream is = split2.getIs();
        URI[] locations = split2.locations();
        if (appendLabel) {
            Path path = Paths.get(locations[0]);
            String parent = path.getParent().toString();
            record.add(new DoubleWritable(labels.indexOf(parent)));
        }

        is.close();
    }

}

From source file:org.datavec.image.recordreader.VideoRecordReader.java

@Override
public void initialize(InputSplit split) throws IOException, InterruptedException {
    if (imageLoader == null) {
        imageLoader = new NativeImageLoader(height, width);
    }/*  www  .ja  va 2s .  c  o  m*/
    if (split instanceof FileSplit) {
        URI[] locations = split.locations();
        if (locations != null && locations.length >= 1) {
            if (locations.length > 1) {
                List<File> allFiles = new ArrayList<>();
                for (URI location : locations) {
                    File iter = new File(location);
                    if (iter.isDirectory()) {
                        allFiles.add(iter);
                        if (appendLabel) {
                            File parentDir = iter.getParentFile();
                            String name = parentDir.getName();
                            if (!labels.contains(name))
                                labels.add(name);

                        }
                    }

                    else {
                        File parent = iter.getParentFile();
                        if (!allFiles.contains(parent) && containsFormat(iter.getAbsolutePath())) {
                            allFiles.add(parent);
                            if (appendLabel) {
                                File parentDir = iter.getParentFile();
                                String name = parentDir.getName();
                                if (!labels.contains(name))
                                    labels.add(name);

                            }
                        }
                    }

                }

                iter = allFiles.iterator();
            } else {
                File curr = new File(locations[0]);
                if (!curr.exists())
                    throw new IllegalArgumentException("Path " + curr.getAbsolutePath() + " does not exist!");
                if (curr.isDirectory())
                    iter = FileUtils.iterateFiles(curr, null, true);
                else
                    iter = Collections.singletonList(curr).iterator();
            }
        }
    }

    else if (split instanceof InputStreamInputSplit) {
        InputStreamInputSplit split2 = (InputStreamInputSplit) split;
        InputStream is = split2.getIs();
        URI[] locations = split2.locations();
        INDArray load = imageLoader.asMatrix(is);
        record = RecordConverter.toRecord(load);
        if (appendLabel) {
            Path path = Paths.get(locations[0]);
            String parent = path.getParent().toString();
            record.add(new DoubleWritable(labels.indexOf(parent)));
        }

        is.close();
    }

}

From source file:org.datavyu.controllers.project.ProjectController.java

private File huntForFile(final File workingDir, final String fileName) {
    // If we can't find the file, we will start looking for the file
    // using the easiest solution first and bump up the complexity as
    // we go along.

    // Solution 1: It is in the same directory as the project file.
    File file = new File(workingDir, fileName);

    if (file.exists()) {
        return file;
    }// w ww  .  jav  a  2  s. c o  m

    IOFileFilter fileNameFilter = FileFilterUtils.nameFileFilter(fileName);

    // Solution 2: It is in a sub-directory of the project file.
    {
        Iterator<File> subFiles = FileUtils.iterateFiles(workingDir, fileNameFilter, TrueFileFilter.TRUE);

        if (subFiles.hasNext()) {
            file = subFiles.next();
        }

        if (file.exists()) {
            return file;
        }
    }

    // Solution 3: It is in the parent of the current directory.
    {
        Iterator<File> subFiles = FileUtils.iterateFiles(workingDir.getParentFile(), fileNameFilter, null);

        if (subFiles.hasNext()) {
            file = subFiles.next();
        }

        if (file.exists()) {
            return file;
        }
    }

    return null;
}

From source file:org.deeplearning4j.models.word2vec.iterator.Word2VecDataFetcher.java

@Override
public void reset() {
    files = FileUtils.iterateFiles(new File(path), null, true);
    cache.clear();

}

From source file:org.deeplearning4j.text.documentiterator.FileDocumentIterator.java

public FileDocumentIterator(File path) {
    if (path.isFile()) {
        iter = Arrays.asList(path).iterator();
        try {/*from   ww w . ja  v a2 s  .c  om*/
            lineIterator = FileUtils.lineIterator(path);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        this.rootDir = path;
    } else {
        iter = FileUtils.iterateFiles(path, null, true);
        try {
            lineIterator = FileUtils.lineIterator(iter.next());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        this.rootDir = path;
    }

}

From source file:org.deeplearning4j.text.documentiterator.FileDocumentIterator.java

@Override
public void reset() {
    if (rootDir.isDirectory())
        iter = FileUtils.iterateFiles(rootDir, null, true);
    else/*from   w  w  w .  ja v  a 2 s .  c  o  m*/
        iter = Arrays.asList(rootDir).iterator();

}

From source file:org.deeplearning4j.text.sentenceiterator.FileSentenceIterator.java

/**
 * Takes a single file or directory// www  .j av  a2s. c  o m
 *
 * @param preProcessor the sentence pre processor
 * @param file         the file or folder to iterate over
 */
public FileSentenceIterator(SentencePreProcessor preProcessor, File file) {
    super(preProcessor);
    this.file = file;
    cache = new java.util.concurrent.ConcurrentLinkedDeque<>();
    if (file.isDirectory())
        fileIterator = FileUtils.iterateFiles(file, null, true);
    else
        fileIterator = Arrays.asList(file).iterator();
}

From source file:org.deeplearning4j.text.sentenceiterator.FileSentenceIterator.java

@Override
public void reset() {
    if (file.isFile())
        fileIterator = Arrays.asList(file).iterator();
    else/*  ww w  .jav  a  2  s.  c  o m*/
        fileIterator = FileUtils.iterateFiles(file, null, true);

}

From source file:org.dice_research.topicmodeling.io.FolderReader.java

public void setDocumentFolder(File documentFolder, IOFileFilter filter) {
    this.documentFolder = documentFolder;
    iterator = FileUtils.iterateFiles(documentFolder, filter, TrueFileFilter.INSTANCE);
}

From source file:org.eclipse.dirigible.repository.local.FileSystemRepository.java

@Override
public List<IEntity> searchName(String root, String parameter, boolean caseInsensitive) throws IOException {

    String workspacePath = LocalWorkspaceMapper.getMappedName(this, root);

    List<IEntity> entities = new ArrayList<IEntity>();

    if ((parameter == null) || "".equals(parameter)) {
        return entities;
    }//from  ww  w . j  ava2 s .  c  o m

    File dir = new File(workspacePath);

    Iterator<File> foundFiles = FileUtils.iterateFiles(dir,
            new WildcardFileFilter("*" + parameter + "*", (caseInsensitive ? INSENSITIVE : SENSITIVE)), TRUE);
    while (foundFiles.hasNext()) {
        File foundFile = foundFiles.next();
        String repositoryName = foundFile.getCanonicalPath().substring(getRepositoryPath().length());
        RepositoryPath localRepositoryPath = new RepositoryPath(repositoryName);
        entities.add(new LocalResource(this, localRepositoryPath));
    }

    return entities;
}