本文整理了Java中org.apache.commons.io.FileUtils.listFilesAndDirs()
方法的一些代码示例,展示了FileUtils.listFilesAndDirs()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileUtils.listFilesAndDirs()
方法的具体详情如下:
包路径:org.apache.commons.io.FileUtils
类名称:FileUtils
方法名:listFilesAndDirs
[英]Finds files within a given directory (and optionally its subdirectories). All files found are filtered by an IOFileFilter.
The resulting collection includes the subdirectories themselves.
[中]查找给定目录(及其子目录)中的文件。找到的所有文件都由IOFileFilter筛选。
生成的集合包括子目录本身。
代码示例来源:origin: commons-io/commons-io
/**
* Allows iteration over the files in given directory (and optionally
* its subdirectories).
* <p>
* All files found are filtered by an IOFileFilter. This method is
* based on {@link #listFilesAndDirs(File, IOFileFilter, IOFileFilter)},
* which supports Iterable ('foreach' loop).
* <p>
* The resulting iterator includes the subdirectories themselves.
*
* @param directory the directory to search in
* @param fileFilter filter to apply when finding files.
* @param dirFilter optional filter to apply when finding subdirectories.
* If this parameter is {@code null}, subdirectories will not be included in the
* search. Use TrueFileFilter.INSTANCE to match all directories.
* @return an iterator of java.io.File for the matching files
* @see org.apache.commons.io.filefilter.FileFilterUtils
* @see org.apache.commons.io.filefilter.NameFileFilter
* @since 2.2
*/
public static Iterator<File> iterateFilesAndDirs(final File directory, final IOFileFilter fileFilter,
final IOFileFilter dirFilter) {
return listFilesAndDirs(directory, fileFilter, dirFilter).iterator();
}
代码示例来源:origin: commons-io/commons-io
@Test
public void testListFilesWithDirs() throws IOException {
final File srcDir = getTestDirectory();
final File subDir1 = new File(srcDir, "subdir");
subDir1.mkdir();
final File subDir2 = new File(subDir1, "subdir2");
subDir2.mkdir();
final File someFile = new File(subDir2, "a.txt");
if (!someFile.getParentFile().exists()) {
throw new IOException("Cannot create file " + someFile
+ " as the parent directory does not exist");
}
final BufferedOutputStream output =
new BufferedOutputStream(new FileOutputStream(someFile));
try {
TestUtils.generateTestData(output, 100);
} finally {
IOUtils.closeQuietly(output);
}
final File subDir3 = new File(subDir2, "subdir3");
subDir3.mkdir();
final Collection<File> files = FileUtils.listFilesAndDirs(subDir1,
new WildcardFileFilter("*.*"), new WildcardFileFilter("*"));
assertEquals(4, files.size());
assertTrue("Should contain the directory.", files.contains(subDir1));
assertTrue("Should contain the directory.", files.contains(subDir2));
assertTrue("Should contain the file.", files.contains(someFile));
assertTrue("Should contain the directory.", files.contains(subDir3));
subDir1.delete();
}
代码示例来源:origin: mulesoft/mule
private void installDistributionFromDirectory() {
muleHome = WORKING_DIRECTORY.resolve(location).toFile();
if (muleHome.exists()) {
return;
}
muleHome.mkdirs();
Arrays.stream(distribution.listFiles())
.forEach(file -> {
try {
if (file.isDirectory()) {
FileUtils.copyDirectory(file, new File(muleHome, file.getName()));
} else {
FileUtils.copyFile(file, new File(muleHome, file.getName()));
}
} catch (IOException e) {
e.printStackTrace();
}
});
FileUtils.listFilesAndDirs(muleHome, TrueFileFilter.TRUE, TrueFileFilter.TRUE)
.forEach(file -> DistroUnzipper.chmodRwx(file));
}
代码示例来源:origin: INRIA/spoon
Collection<File> dirs = FileUtils.listFilesAndDirs(mClassFilesDir, DirectoryFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
dirs.stream()
.filter(d -> d.exists() && FileUtils.listFiles(d, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE).isEmpty())
代码示例来源:origin: gradle.plugin.com.s390x/gogradle
public static Collection<File> listAllDescendents(File dir) {
return FileUtils.listFilesAndDirs(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
}
代码示例来源:origin: simpligility/maven-repository-tools
/**
* Determine if it is a leaf directory with artifacts in it. Criteria used is that there is no subdirectory.
*
* @param subDirectory
* @return
*/
private static boolean isLeafVersionDirectory( File subDirectory )
{
boolean isLeafVersionDirectory;
Collection<File> subDirectories =
FileUtils.listFilesAndDirs( subDirectory,
(IOFileFilter) VisibleDirectoryFileFilter.DIRECTORY,
(IOFileFilter) VisibleDirectoryFileFilter.DIRECTORY );
// it finds at least itself so have to check for > 1
isLeafVersionDirectory = subDirectories.size() > 1 ? false : true;
return isLeafVersionDirectory;
}
代码示例来源:origin: org.onosproject/onlab-thirdparty
/**
* Allows iteration over the files in given directory (and optionally
* its subdirectories).
* <p>
* All files found are filtered by an IOFileFilter. This method is
* based on {@link #listFilesAndDirs(File, IOFileFilter, IOFileFilter)},
* which supports Iterable ('foreach' loop).
* <p>
* The resulting iterator includes the subdirectories themselves.
*
* @param directory the directory to search in
* @param fileFilter filter to apply when finding files.
* @param dirFilter optional filter to apply when finding subdirectories.
* If this parameter is {@code null}, subdirectories will not be included in the
* search. Use TrueFileFilter.INSTANCE to match all directories.
* @return an iterator of java.io.File for the matching files
* @see org.apache.commons.io.filefilter.FileFilterUtils
* @see org.apache.commons.io.filefilter.NameFileFilter
* @since 2.2
*/
public static Iterator<File> iterateFilesAndDirs(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
return listFilesAndDirs(directory, fileFilter, dirFilter).iterator();
}
代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded
/**
* Allows iteration over the files in given directory (and optionally
* its subdirectories).
* <p>
* All files found are filtered by an IOFileFilter. This method is
* based on {@link #listFilesAndDirs(File, IOFileFilter, IOFileFilter)},
* which supports Iterable ('foreach' loop).
* <p>
* The resulting iterator includes the subdirectories themselves.
*
* @param directory the directory to search in
* @param fileFilter filter to apply when finding files.
* @param dirFilter optional filter to apply when finding subdirectories.
* If this parameter is {@code null}, subdirectories will not be included in the
* search. Use TrueFileFilter.INSTANCE to match all directories.
* @return an iterator of java.io.File for the matching files
* @see org.apache.commons.io.filefilter.FileFilterUtils
* @see org.apache.commons.io.filefilter.NameFileFilter
* @since 2.2
*/
public static Iterator<File> iterateFilesAndDirs(final File directory, final IOFileFilter fileFilter,
final IOFileFilter dirFilter) {
return listFilesAndDirs(directory, fileFilter, dirFilter).iterator();
}
代码示例来源:origin: Nextdoor/bender
/**
* Allows iteration over the files in given directory (and optionally
* its subdirectories).
* <p>
* All files found are filtered by an IOFileFilter. This method is
* based on {@link #listFilesAndDirs(File, IOFileFilter, IOFileFilter)},
* which supports Iterable ('foreach' loop).
* <p>
* The resulting iterator includes the subdirectories themselves.
*
* @param directory the directory to search in
* @param fileFilter filter to apply when finding files.
* @param dirFilter optional filter to apply when finding subdirectories.
* If this parameter is <code>null</code>, subdirectories will not be included in the
* search. Use TrueFileFilter.INSTANCE to match all directories.
* @return an iterator of java.io.File for the matching files
* @see org.apache.commons.io.filefilter.FileFilterUtils
* @see org.apache.commons.io.filefilter.NameFileFilter
* @since 2.2
*/
public static Iterator<File> iterateFilesAndDirs(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
return listFilesAndDirs(directory, fileFilter, dirFilter).iterator();
}
代码示例来源:origin: io.github.stephenc.docker/docker-client-shaded
/**
* Allows iteration over the files in given directory (and optionally
* its subdirectories).
* <p>
* All files found are filtered by an IOFileFilter. This method is
* based on {@link #listFilesAndDirs(File, IOFileFilter, IOFileFilter)},
* which supports Iterable ('foreach' loop).
* <p>
* The resulting iterator includes the subdirectories themselves.
*
* @param directory the directory to search in
* @param fileFilter filter to apply when finding files.
* @param dirFilter optional filter to apply when finding subdirectories.
* If this parameter is {@code null}, subdirectories will not be included in the
* search. Use TrueFileFilter.INSTANCE to match all directories.
* @return an iterator of java.io.File for the matching files
* @see org.apache.commons.io.filefilter.FileFilterUtils
* @see org.apache.commons.io.filefilter.NameFileFilter
* @since 2.2
*/
public static Iterator<File> iterateFilesAndDirs(final File directory, final IOFileFilter fileFilter,
final IOFileFilter dirFilter) {
return listFilesAndDirs(directory, fileFilter, dirFilter).iterator();
}
代码示例来源:origin: sterodium/selenium-grid-extensions
private File findFolderRecursively(File folderToScan, final String folderToFind) {
// filtering out empty arguments
if (folderToFind.isEmpty()) {
return folderToScan;
}
// collecting all the sub-folders recursively
Collection<File> foundFolders = listFilesAndDirs(folderToScan, DIRECTORY, TRUE);
// filtering all the folders by folder name
foundFolders = filter(foundFolders, new Predicate<File>() {
@Override
public boolean apply(File file) {
String fileName = file.getAbsolutePath();
return fileName.endsWith("/" + folderToFind) || fileName.endsWith("\\" + folderToFind);
}
});
return foundFolders.isEmpty() ? null : foundFolders.iterator().next();
}
代码示例来源:origin: io.teecube.tic/tic-bw6
protected File getThorLaunchDirectory() throws FileNotFoundException {
if (thorLaunchDirectory != null) {
return thorLaunchDirectory;
}
File bundlePoolPlugins = new File(eclipsePlatformHome, "org.eclipse.equinox.p2.touchpoint.eclipse/plugins");
IOFileFilter wildcardFilter = new WildcardFileFilter("com.tibco.bw.thor.launch_*");
Collection<File> thorLaunchDirectories = FileUtils.listFilesAndDirs(bundlePoolPlugins, wildcardFilter, wildcardFilter);
if (thorLaunchDirectories.size() < 2) {
throw new FileNotFoundException("Unable to find Thor Launch directory");
}
thorLaunchDirectory = thorLaunchDirectories.toArray(new File[0])[1];
return thorLaunchDirectory;
}
代码示例来源:origin: winstonli/writelatex-git-bridge
private static Set<Path> getChildren(File f0, Path f0Base) {
return FileUtils.listFilesAndDirs(
f0,
TrueFileFilter.TRUE,
TrueFileFilter.TRUE
).stream(
).map(
File::getAbsolutePath
).map(
Paths::get
).map(p ->
f0Base.relativize(p)
).filter(p ->
!p.toString().isEmpty()
).collect(
Collectors.toSet()
);
}
代码示例来源:origin: Refactoring-Bot/Refactoring-Bot
/**
* This method finds the Root-Folder of a repository. That is the folder that
* contains the src folder.
*
* @param repoFolder
* @return rootFolder
* @throws IOException
*/
public String findSrcFolder(String repoFolder) throws IOException {
// Get root folder of project
File dir = new File(repoFolder);
// Get paths to all java files of the project
List<File> files = (List<File>) FileUtils.listFilesAndDirs(dir, TrueFileFilter.INSTANCE,
TrueFileFilter.INSTANCE);
for (File file : files) {
if (file.isDirectory() && file.getName().equals("src")) {
return file.getAbsolutePath();
}
}
String noSrcFolderFoundErrorMsg = "No src-folder found inside this java-project!";
logger.error(noSrcFolderFoundErrorMsg);
throw new FileNotFoundException(noSrcFolderFoundErrorMsg);
}
}
代码示例来源:origin: net.serenity-bdd/serenity-model
public int maxDepth() {
if (!rootDirectory.isDirectory()) { return 0; }
Collection<File> directoryContents = FileUtils.listFilesAndDirs(rootDirectory,
new NotFileFilter(TrueFileFilter.INSTANCE),
normalDirectoriesOnly());
int maxDepth = 0;
for(File file : directoryContents) {
String relativePath = file.getPath().replace(rootDirectory.getPath(),"");
int depth = Splitter.on(File.separator).trimResults().splitToList(relativePath).size() - 1;
if (depth > maxDepth) {
maxDepth = depth;
}
}
return maxDepth;
}
代码示例来源:origin: simpligility/maven-repository-tools
public static Collection<File> getLeafDirectories( File repoPath )
{
// Using commons-io, if performance or so is a problem it might be worth looking at the Java 8 streams API
// e.g. http://blog.jooq.org/2014/01/24/java-8-friday-goodies-the-new-new-io-apis/
// not yet though..
Collection<File> subDirectories =
FileUtils.listFilesAndDirs( repoPath, DirectoryFileFilter.DIRECTORY,
VisibleDirectoryFileFilter.DIRECTORY );
Collection<File> leafDirectories = new ArrayList<File>();
for ( File subDirectory : subDirectories )
{
if ( isLeafVersionDirectory( subDirectory ) && subDirectory != repoPath )
{
leafDirectories.add( subDirectory );
}
}
return leafDirectories;
}
代码示例来源:origin: com.github.crabs-hue.commons/commons-file
/**
* Check if the content of a folder is empty (i.e. doesn't contain any folder nor file)
*
* @param folder the folder
* @return true if the folder is empty, false otherwise
*/
public static boolean isFolderEmpty(final File folder) {
Validate.notNull(folder);
Collection<File> files = FileUtils.listFilesAndDirs(folder, FileFilterUtils.trueFileFilter(), FileFilterUtils.trueFileFilter());
// files contains the parent directory
boolean ret = files.size() <= 1;
logger.info("Folder [{}] is empty ? : {}", folder, ret);
return ret;
}
代码示例来源:origin: com.paypal.butterfly/butterfly-utilities
Collection<File> allFolders = FileUtils.listFilesAndDirs(searchRootFolder, new NotFileFilter(TrueFileFilter.INSTANCE), (recursive ? TrueFileFilter.INSTANCE : DirectoryFileFilter.DIRECTORY));
allFolders.remove(searchRootFolder);
for (File folder : allFolders) {
代码示例来源:origin: org.codehaus.sonar-plugins.java/java-squid
Collection<File> dirs = FileUtils.listFilesAndDirs(dir, new AndFileFilter(wilcardPatternFileFilter, DirectoryFileFilter.DIRECTORY), subdirectories);
代码示例来源:origin: org.sonarsource.java/java-squid
Collection<File> dirs = FileUtils.listFilesAndDirs(dir, new AndFileFilter(wilcardPatternFileFilter, DirectoryFileFilter.DIRECTORY), subdirectories);
内容来源于网络,如有侵权,请联系作者删除!