org.apache.commons.io.FileUtils.iterateFiles()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(10.6k)|赞(0)|评价(0)|浏览(400)

本文整理了Java中org.apache.commons.io.FileUtils.iterateFiles()方法的一些代码示例,展示了FileUtils.iterateFiles()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileUtils.iterateFiles()方法的具体详情如下:
包路径:org.apache.commons.io.FileUtils
类名称:FileUtils
方法名:iterateFiles

FileUtils.iterateFiles介绍

[英]Allows iteration over the files in given directory (and optionally its subdirectories).

All files found are filtered by an IOFileFilter. This method is based on #listFiles(File,IOFileFilter,IOFileFilter), which supports Iterable ('foreach' loop).
[中]允许对给定目录(以及可选的子目录)中的文件进行迭代。
找到的所有文件都由IOFileFilter筛选。此方法基于#listFiles(File,IOFileFilter,IOFileFilter),它支持Iterable('foreach'循环)。

代码示例

代码示例来源:origin: deeplearning4j/nd4j

/**
 * Check if a file exists in the path
 * @param name the name of the file
 * @return true if the name exists
 * false otherwise
 */
public static boolean nameExistsInPath(String name) {
  String path = System.getenv(PATH_ENV_VARIABLE);
  String[] dirs = path.split(File.pathSeparator);
  for (String dir : dirs) {
    File dirFile = new File(dir);
    if (!dirFile.exists())
      continue;
    if (dirFile.isFile() && dirFile.getName().equals(name))
      return true;
    else {
      Iterator<File> files = FileUtils.iterateFiles(dirFile, null, false);
      while (files.hasNext()) {
        File curr = files.next();
        if (curr.getName().equals(name))
          return true;
      }
    }
  }
  return false;
}

代码示例来源:origin: commons-io/commons-io

final Iterator<File> files = FileUtils.iterateFiles(subDir,
    new WildcardFileFilter("*.*"),
    new WildcardFileFilter("*"));

代码示例来源:origin: commons-io/commons-io

@Test
public void testIterateFilesByExtension() throws Exception {
  final String[] extensions = { "xml", "txt" };
  Iterator<File> files = FileUtils.iterateFiles(getLocalTestDirectory(), extensions, false);
  Collection<String> filenames = filesToFilenames(files);
  assertEquals(1, filenames.size());
  assertTrue(filenames.contains("dummy-build.xml"));
  assertFalse(filenames.contains("README"));
  assertFalse(filenames.contains("dummy-file.txt"));
  files = FileUtils.iterateFiles(getLocalTestDirectory(), extensions, true);
  filenames = filesToFilenames(files);
  assertEquals(4, filenames.size());
  assertTrue(filenames.contains("dummy-file.txt"));
  assertFalse(filenames.contains("dummy-index.html"));
  files = FileUtils.iterateFiles(getLocalTestDirectory(), null, false);
  filenames = filesToFilenames(files);
  assertEquals(2, filenames.size());
  assertTrue(filenames.contains("dummy-build.xml"));
  assertTrue(filenames.contains("README"));
  assertFalse(filenames.contains("dummy-file.txt"));
}

代码示例来源:origin: square/spoon

@Test
public void correctRenderingOfNonLatinCharacters() throws IOException {
 SpoonSummary summary = prepareNonLatinSummary();
 File folder = testFolder.getRoot();
 CharsetDecoder utf8Decoder = StandardCharsets.UTF_8.newDecoder();
 HtmlRenderer htmlRenderer = new HtmlRenderer(summary, SpoonUtils.GSON, folder);
 htmlRenderer.render();
 setDefaultCharset(StandardCharsets.US_ASCII);
 Iterator<File> it = FileUtils.iterateFiles(folder, FILE_EXTENSIONS_TO_CHECK, true);
 File nextFile = null;
 try {
  while (it.hasNext()) {
   nextFile = it.next();
   decode(nextFile, utf8Decoder);
  }
 } catch (MalformedInputException ex) {
  throw new IllegalStateException(String.format("Found wrong file [%s]", nextFile.getName()));
 } finally {
  setDefaultCharset(null);
 }
}

代码示例来源:origin: dermotte/LIRE

/**
 * Returns all images from a directory in an array. Image files are identified by their suffix being from {.png, .jpg, .jpeg, .gif} in case insensitive manner.
 *
 * @param directory                 the directory to start with
 * @param descendIntoSubDirectories should we include sub directories?
 * @return an ArrayList<String> containing all the files or null if none are found..
 * @throws IOException
 */
public static ArrayList<String> getAllImages(File directory, boolean descendIntoSubDirectories) throws IOException {
  ArrayList<String> resultList = new ArrayList<String>(256);
  IOFileFilter includeSubdirectories = TrueFileFilter.INSTANCE;
  if (!descendIntoSubDirectories) includeSubdirectories = null;
  Iterator<File> fileIterator = org.apache.commons.io.FileUtils.iterateFiles(directory, fileFilter, includeSubdirectories);
  while (fileIterator.hasNext()) {
    File next = fileIterator.next();
    resultList.add(next.getCanonicalPath());
  }
  if (resultList.size() > 0)
    return resultList;
  else
    return null;
}

代码示例来源:origin: dermotte/LIRE

/**
 * Returns all images from a directory in an array. Image files are identified by their suffix being from {.png, .jpg, .jpeg, .gif} in case insensitive manner.
 *
 * @param directory                 the directory to start with
 * @param descendIntoSubDirectories should we include sub directories?
 * @return an ArrayList<String> containing all the files or null if none are found..
 * @throws IOException
 */
public static ArrayList<String> getAllImages(File directory, boolean descendIntoSubDirectories) throws IOException {
  ArrayList<String> resultList = new ArrayList<String>(256);
  IOFileFilter includeSubdirectories = TrueFileFilter.INSTANCE;
  if (!descendIntoSubDirectories) includeSubdirectories = null;
  Iterator<File> fileIterator = org.apache.commons.io.FileUtils.iterateFiles(directory, fileFilter, includeSubdirectories);
  while (fileIterator.hasNext()) {
    File next = fileIterator.next();
    resultList.add(next.getCanonicalPath());
  }
  if (resultList.size() > 0)
    return resultList;
  else
    return null;
}

代码示例来源:origin: winterDroid/android-drawable-importer-intellij-plugin

progressIndicator.checkCanceled();
final Iterator<File> fileIterator = FileUtils.iterateFiles(tempDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
while (fileIterator.hasNext()) {
  File file = fileIterator.next();

代码示例来源:origin: dermotte/LIRE

Iterator<File> fileIterator = FileUtils.iterateFiles(new File("."), new String[]{"msd"}, false);
while (fileIterator.hasNext()) {
  File f = fileIterator.next();

代码示例来源:origin: dermotte/LIRE

Iterator<File> fileIterator = FileUtils.iterateFiles(new File("."), new String[]{"msd"}, false);
while (fileIterator.hasNext()) {
  File f = fileIterator.next();

代码示例来源:origin: com.technophobia.substeps/substeps-core

public Iterator<Class<?>> fromPath(final String path) {
    final File directory = new File(path);
    final Iterator<File> files = FileUtils.iterateFiles(directory,
        new String[] { "class" }, true);
    final Iterator<Class<?>> unsafeTransformedClasses = Iterators
        .transform(files, classLoader);

    return Iterators.filter(unsafeTransformedClasses,
        Predicates.and(Predicates.notNull(), predicate));
  }
}

代码示例来源:origin: OpenNMS/opennms

protected static void cleanUpImports() {
  final Iterator<File> fileIterator = FileUtils.iterateFiles(new File("target/test/opennms-home/etc/imports"), null, true);
  while (fileIterator.hasNext()) {
    if(!fileIterator.next().delete()) {
      LOG.warn("Could not delete file: {}", fileIterator.next().getPath());
    }
  }
}

代码示例来源:origin: ru.yandex.qatools.allure/allure-java-inject

protected static Iterator<File> getClassFiles(File directory) {
  if (directory.exists() && directory.isDirectory() && directory.canRead()) {
    return FileUtils.iterateFiles(directory,
        new RegexFileFilter(CLASS_FILTER), CanWriteFileFilter.CAN_WRITE);
  } else {
    return Collections.<File>emptyList().iterator();
  }
}

代码示例来源:origin: appfuse/appfuse

private void refactorNonPackageFiles() {
  try {
    String[] extensions = {"java", "page", "application", "properties", "tld", "xml"};
    Iterator filesInMain = FileUtils.iterateFiles(new File(this.workBaseDir), extensions, true);
    while (filesInMain.hasNext()) {
      File f = (File) filesInMain.next();
      changePackageNamesInFile(f.getAbsolutePath(), RenamePackages.SAVE_FILE);
    }
  } catch (IOException ioex) {
    log.error("IOException: " + ioex.getMessage());
  }
}

代码示例来源:origin: jpatanooga/Canova

@Override
public void initialize(InputSplit split) throws IOException, InterruptedException {
  this.locations = split.locations();
  if(locations != null && locations.length > 0) {
    iter = FileUtils.iterateFiles(new File(locations[0]),null,true);
  }
}

代码示例来源:origin: indeedeng/imhotep

private void scanExistingFiles() throws IOException {
  final Iterator<File> filesInCache;
  final int prefixLen;
  
  prefixLen = localCacheDir.getCanonicalPath().length() + DELIMITER.length();
  filesInCache = FileUtils.iterateFiles(localCacheDir, 
                     TrueFileFilter.INSTANCE, 
                     TrueFileFilter.INSTANCE);
  while (filesInCache.hasNext()) {
    final File cachedFile = filesInCache.next();
    final String path = cachedFile.getCanonicalPath();
    String cachePath = path.substring(prefixLen);
    cache.put(cachePath, cachedFile);
  }
}

代码示例来源:origin: USPTO/PatentPublicData

/**
 * Iterator for Files in a Directory.
 * 
 * @param directory
 * @return
 * @throws FileNotFoundException 
 */
public static Iterator<File> getFileIterator(File file) throws FileNotFoundException {
  if (file.isDirectory()){
    return FileUtils.iterateFiles(file, new String[] { "zip" }, true);
  } else {
    return FileIterator.getFileIterator(Arrays.asList(new String[]{file.getAbsolutePath()}));
  }
}

代码示例来源:origin: de.icongmbh.oss.maven.plugins/javassist-maven-plugin

protected Iterator<String> iterateClassnames(final String dir) {
  final String[] extensions = { ".class" };
  final File directory = new File(dir);
  IOFileFilter fileFilter = new SuffixFileFilter(extensions);
  final IOFileFilter dirFilter = TrueFileFilter.INSTANCE;
  return ClassnameExtractor.iterateClassnames(directory, FileUtils.iterateFiles(directory, fileFilter, dirFilter));
}

代码示例来源:origin: org.codehaus.sonar.plugins/sonar-plugin-sources

private void parseDirectory(File directory, boolean unitTest) throws IOException {
 if (directory.exists()) {
  Iterator fileIterator = FileUtils.iterateFiles(directory, new String[]{"java"}, true);
  while (fileIterator.hasNext()) {
   File file = (File) fileIterator.next();
   String filename = StringUtils.substringBeforeLast(file.getName(), ".");
   String source = FileUtils.readFileToString(file);
   getMeasuresRecorder().createClassSource(extractPackageName(file, directory), filename, source, unitTest);
  }
 }
}

代码示例来源:origin: dswarm/dswarm

public static List<URI> collectResources(final String root) {

    final List<URI> resources = new ArrayList<>();

    final File folder = new File(DMPPersistenceTestUtils.getTestResourcesBasePath(root));
    final IOFileFilter fileFilter = new RegexFileFilter(".*task\\.((.*?)(?<!result)\\.){0,}json"); // find all *task*.json but
    // without result in it
    final Iterator<File> it = FileUtils.iterateFiles(folder, fileFilter, null);

    while (it.hasNext()) {
      resources.add(it.next().toURI());
    }

    return resources;
  }
}

代码示例来源:origin: ClearTK/cleartk

public static void testParameterDefinitions(String outputDirectory, String... excludeFiles)
  throws ClassNotFoundException {
 IOFileFilter includeFilter = new SuffixFileFilter(".java");
 if (excludeFiles != null) {
  IOFileFilter excludeFilter = FileFilterUtils.notFileFilter(new SuffixFileFilter(excludeFiles));
  includeFilter = FileFilterUtils.and(excludeFilter, includeFilter);
 }
 Iterator<File> files = org.apache.commons.io.FileUtils.iterateFiles(
   new File(outputDirectory),
   includeFilter,
   TrueFileFilter.INSTANCE);
 testParameterDefinitions(files);
}

相关文章

FileUtils类方法