You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2006/01/01 17:25:26 UTC
svn commit: r360495 - in /jakarta/commons/proper/io/trunk: RELEASE-NOTES.txt
project.xml src/java/org/apache/commons/io/FileUtils.java
src/test/org/apache/commons/io/FileUtilsTestCase.java
Author: scolebourne
Date: Sun Jan 1 08:25:19 2006
New Revision: 360495
URL: http://svn.apache.org/viewcvs?rev=360495&view=rev
Log:
FileUtils methods to provide iterators over directory contents
rfe 38083, from Jim Harrington
Modified:
jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
jakarta/commons/proper/io/trunk/project.xml
jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
Modified: jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=360495&r1=360494&r2=360495&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt Sun Jan 1 08:25:19 2006
@@ -39,6 +39,9 @@
- FileUtils.contentEquals(File,File)
Performance improved by adding length and file location checking
+- FileUtils.iterateFiles
+ Two new method to provide direct access to iterators over files
+
Feedback
--------
Modified: jakarta/commons/proper/io/trunk/project.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/project.xml?rev=360495&r1=360494&r2=360495&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/project.xml (original)
+++ jakarta/commons/proper/io/trunk/project.xml Sun Jan 1 08:25:19 2006
@@ -176,6 +176,9 @@
<name>Chris Eldredge</name>
</contributor>
<contributor>
+ <name>Jim Harrington</name>
+ </contributor>
+ <contributor>
<name>Thomas Ledoux</name>
</contributor>
<contributor>
Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java?rev=360495&r1=360494&r2=360495&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java Sun Jan 1 08:25:19 2006
@@ -28,6 +28,7 @@
import java.util.Collection;
import java.util.Date;
import java.util.List;
+import java.util.Iterator;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.FalseFileFilter;
@@ -65,6 +66,7 @@
* @author Stephen Colebourne
* @author Ian Springer
* @author Chris Eldredge
+ * @author Jim Harrington
* @version $Id$
*/
public class FileUtils {
@@ -226,6 +228,26 @@
return files;
}
+ /**
+ * <p>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 {@link #listFiles(File, IOFileFilter, IOFileFilter)}.
+ * </p>
+ *
+ * @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 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 Commons IO 1.2
+ */
+ public static Iterator iterateFiles(
+ File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
+ return listFiles(directory, fileFilter, dirFilter).iterator();
+ }
/**
* Converts an array of file extensions to suffixes for use
@@ -264,6 +286,22 @@
(recursive ? TrueFileFilter.INSTANCE : FalseFileFilter.INSTANCE));
}
+ /**
+ * Allows iteration over the files in a given directory (and optionally
+ * its subdirectories) which match an array of extensions. This method
+ * is based on {@link #listFiles(File, String[], boolean)}.
+ *
+ * @param directory the directory to search in
+ * @param extensions an array of extensions, ex. {"java","xml"}. If this
+ * parameter is null, all files are returned.
+ * @param recursive if true all subdirectories are searched as well
+ * @return an iterator of java.io.File with the matching files
+ * @since Commons IO 1.2
+ */
+ public static Iterator iterateFiles(
+ File directory, String[] extensions, boolean recursive) {
+ return listFiles(directory, extensions, recursive).iterator();
+ }
/**
* <p>Compare the contents of two files to determine if they are equal or
Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java?rev=360495&r1=360494&r2=360495&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java (original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java Sun Jan 1 08:25:19 2006
@@ -24,12 +24,17 @@
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.List;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.HashMap;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import org.apache.commons.io.testtools.FileBasedTestCase;
+import org.apache.commons.io.filefilter.WildcardFilter;
/**
* This is used to test FileUtils for correctness.
@@ -37,6 +42,7 @@
* @author Peter Donald
* @author Matthew Hawthorne
* @author Stephen Colebourne
+ * @author Jim Harrington
* @version $Id$
* @see FileUtils
*/
@@ -657,6 +663,81 @@
assertEquals("FileUtils.touch() changed lastModified", false, y2k == file.lastModified());
assertEquals("FileUtils.touch() changed lastModified to more than now-3s", true, file.lastModified() >= (now - 3000));
assertEquals("FileUtils.touch() changed lastModified to less than now+3s", true, file.lastModified() <= (now + 3000));
+ }
+
+ public void testListFiles() throws Exception {
+ File srcDir = getTestDirectory();
+ File subDir = new File(srcDir, "list_test" );
+ subDir.mkdir();
+
+ String[] fileNames = {"a.txt", "b.txt", "c.txt", "d.txt", "e.txt", "f.txt"};
+ int[] fileSizes = {123, 234, 345, 456, 678, 789};
+
+ for (int i = 0; i < fileNames.length; ++i) {
+ File theFile = new File(subDir, fileNames[i]);
+ createFile(theFile, fileSizes[i]);
+ }
+
+ Collection files = FileUtils.listFiles(subDir,
+ new WildcardFilter("*.*"),
+ new WildcardFilter("*"));
+
+ int count = files.size();
+ Object[] fileObjs = files.toArray();
+
+ assertEquals(files.size(), fileNames.length);
+
+ Map foundFileNames = new HashMap();
+
+ for (int i = 0; i < count; ++i) {
+ boolean found = false;
+ for(int j = 0; (( !found ) && (j < fileNames.length)); ++j) {
+ if ( fileNames[j].equals(((File) fileObjs[i]).getName())) {
+ foundFileNames.put(fileNames[j], fileNames[j]);
+ found = true;
+ }
+ }
+ }
+
+ assertEquals(foundFileNames.size(), fileNames.length);
+
+ subDir.delete();
+ }
+
+ public void testIterateFiles() throws Exception {
+ File srcDir = getTestDirectory();
+ File subDir = new File(srcDir, "list_test" );
+ subDir.mkdir();
+
+ String[] fileNames = {"a.txt", "b.txt", "c.txt", "d.txt", "e.txt", "f.txt"};
+ int[] fileSizes = {123, 234, 345, 456, 678, 789};
+
+ for (int i = 0; i < fileNames.length; ++i) {
+ File theFile = new File(subDir, fileNames[i]);
+ createFile(theFile, fileSizes[i]);
+ }
+
+ Iterator files = FileUtils.iterateFiles(subDir,
+ new WildcardFilter("*.*"),
+ new WildcardFilter("*"));
+
+ Map foundFileNames = new HashMap();
+
+ while (files.hasNext()) {
+ boolean found = false;
+ String fileName = ((File) files.next()).getName();
+
+ for (int j = 0; (( !found ) && (j < fileNames.length)); ++j) {
+ if ( fileNames[j].equals(fileName)) {
+ foundFileNames.put(fileNames[j], fileNames[j]);
+ found = true;
+ }
+ }
+ }
+
+ assertEquals(foundFileNames.size(), fileNames.length);
+
+ subDir.delete();
}
public void testReadFileToString() throws Exception {
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org