<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Information Centre &#187; util</title>
	<atom:link href="http://my.center-of.info/tag/util/feed/" rel="self" type="application/rss+xml" />
	<link>http://my.center-of.info</link>
	<description>“Wenn etwas schon da war, wie kann man es dann patentieren?” D.E.Knuth, 2002</description>
	<lastBuildDate>Mon, 28 Sep 2009 23:04:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Commons-IO: DirectoryWalker</title>
		<link>http://my.center-of.info/2009/07/19/commons-io-directorywalker/</link>
		<comments>http://my.center-of.info/2009/07/19/commons-io-directorywalker/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 19:43:27 +0000</pubDate>
		<dc:creator>Haf</dc:creator>
				<category><![CDATA[JEE]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[util]]></category>

		<guid isPermaLink="false">http://my.center-of.info/?p=132</guid>
		<description><![CDATA[Commons-IO bzw. allg. Apache Commons ist eine schöne Ansammlung von nützlichen Bibliotheken und Funktionen.
Darunter ist auch Commons-IO zu finden. Daraus will ich heute eine spezielle Lösung vorstellen.
Hat man folgendes Problem:
Möchte man einen Ordner, inklusive dessen Unterordner, nach bestimmten Dateien suchen und dann diese vielleicht nach Last-Modified sortiert, bietet Commons-IO eine Lösung an.

Um den/die Ordner durchzulaufen [...]]]></description>
			<content:encoded><![CDATA[<p>Commons-IO bzw. allg. <a href="http://commons.apache.org/" target="_blank" title="Apache Commons">Apache Commons</a> ist eine schöne Ansammlung von nützlichen Bibliotheken und Funktionen.<br />
Darunter ist auch Commons-IO zu finden. Daraus will ich heute eine spezielle Lösung vorstellen.<br />
Hat man folgendes Problem:<br />
Möchte man einen Ordner, inklusive dessen Unterordner, nach bestimmten Dateien suchen und dann diese vielleicht nach Last-Modified sortiert, bietet Commons-IO eine Lösung an.<br />
<span id="more-132"></span></p>
<p>Um den/die Ordner durchzulaufen existiert ein <a href="http://commons.apache.org/io/api-release/org/apache/commons/io/DirectoryWalker.html" target="_blank" title="Commons-IO API: DirectoryWalker">DirectoryWalker</a>.<br />
Um den abstrakten DirectoryWalker nutzen zu können könnte man z.B. eine folgende Implementierung nutzen:</p>
<pre class="brush: java">
public class TestDirectoryWalker extends DirectoryWalker {

	public TestDirectoryWalker(IOFileFilter dirFilter, IOFileFilter fileFilter, int depth) {
		super(dirFilter, fileFilter, depth);
	}

        /**
          * This method sets the start directory and starts the DirectoryWalker.
          */
	public List search(File startDir) throws IOException {

		List results = new ArrayList();
		walk(startDir, results);
		return results;
	}

	protected boolean handleDirectory(File directory, int depth, Collection results) {
		// return true, cause every sub directory is allowed
		return true;
	}

	protected void handleFile(File file, int depth, Collection results) {		

                // we want only the files with the name &quot;abc.txt&quot; in the
                // subdirectories. This files will be inserted in the given
                // result collection. This collection is the same as the
                // created List in search(File)
		String filename = file.getName();
		if(&quot;abc.txt&quot;.equalsIgnoreCase(filename))
			results.add(file);
	}
}
</pre>
<p>Wie man sehen kann, kann man innerhalb weniger Zeilen einen DirectoryWalker schreiben, der alle Unterordner durchgeht und alle Dateien mit dem Titel &#8220;abc.txt&#8221; liefert.<br />
Die Nutzung ist genauso simpel:</p>
<pre class="brush: java">
TestDirectoryWalker idw = new TestDirectoryWalker(DirectoryFileFilter.DIRECTORY, FileFilterUtils.suffixFileFilter(&quot;.txt&quot;), 3);

		String startDir = &quot;D:/Temp/.../blub&quot;;
		List files = null;
		try {
                        // search the directory for the wanted files
			files = idw.search(new File(startDir));
			System.out.println(&quot;Files: #&quot; + files.size());

                        // sort the resulting files using the LastModifiedFileComparator
			System.out.println(&quot;first file: &quot; + files.get(0));
			Collections.sort(files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);

			System.out.println(&quot;oldest file: &quot; + files.get(0));
		} catch (IOException e) {
			e.printStackTrace();
		}
</pre>
<p>Der Konstruktor TestDirectoryWalker(<a href="http://commons.apache.org/io/api-release/org/apache/commons/io/filefilter/IOFileFilter.html" target="_blank" title="Apache Commons-IO API: IOFileFilter">IOFileFilter</a>, <a href="http://commons.apache.org/io/api-release/org/apache/commons/io/filefilter/IOFileFilter.html" target="_blank" title="Apache Commons-IO API: IOFileFilter">IOFileFilter</a>, int) erwartet zwei Filter, einen für die Ordner und einen für die Dateien. Die <a href="http://commons.apache.org/io/api-release/org/apache/commons/io/filefilter/IOFileFilter.html" target="_blank" title="Apache Commons-IO API: IOFileFilter">Auswahl</a> ist umfangreich, ansonsten kann man auch seinen eigenen schreiben.</p>
<p>Als letztes ist vielleicht noch die Zeile</p>
<pre class="brush: java">
Collections.sort(files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);
</pre>
<p>interessant. <a href="http://commons.apache.org/io/api-release/org/apache/commons/io/comparator/LastModifiedFileComparator.html" target="_blank" title="Apache Commons IO API: LastModifiedFileComparator">LastModifiedFileComparator</a> ist wie der Name schon andeutet, eine Klasse welches <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html" target="_blank" title="J2SE API: Comparator">Comparator</a> implementiert bzgl. <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#lastModified()" target="_blank" title="J2SE API: File#lastModified()">File.lastModified()</a>. Die Liste mit den gefundenen Dateien wird aufsteigend sortiert.</p>
<p>Innerhalb weniger Minuten kann man verschiedene Ordner nach speziellen Dateien suchen. Dank <a href="http://commons.apache.org/io/" target="_blank" title="Apache Commons IO">Apache Commons IO</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://my.center-of.info/2009/07/19/commons-io-directorywalker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
