<?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>Cow Computing &#187; Regular Expression</title>
	<atom:link href="http://www.cowcomputing.com/tag/regular-expression/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cowcomputing.com</link>
	<description>Share Info about Cloud Computing &#38; Programming</description>
	<lastBuildDate>Thu, 22 Jul 2010 17:10:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Finding Files with particular extension or Pattern in Java</title>
		<link>http://www.cowcomputing.com/2010/03/04/finding-files-with-particular-extension-or-pattern-in-java/</link>
		<comments>http://www.cowcomputing.com/2010/03/04/finding-files-with-particular-extension-or-pattern-in-java/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 09:16:53 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[Pattern]]></category>
		<category><![CDATA[RegExp]]></category>
		<category><![CDATA[Regular Expression]]></category>

		<guid isPermaLink="false">http://www.cowcomputing.com/?p=196</guid>
		<description><![CDATA[Often, to find a file with a particular extension or naming pattern within a directory, we might implement as below: // The directory which search would be conducted File directoryForSearch = new File("C:\folder"); // This may not be the best way to accomplish the task, please bear me with it File[] allFile = directoryForSearch.listFiles(); File[] [...]]]></description>
			<content:encoded><![CDATA[<p>Often, to find a file with a particular extension or naming pattern within a directory, we might implement as below:</p>
<pre class="brush:java">// The directory which search would be conducted
File directoryForSearch = new File("C:\folder");

// This may not be the best way to accomplish the task, please bear me with it
File[] allFile = directoryForSearch.listFiles();
File[] resultFile = new File[allFile.length];
int resultCount = 0;

// loop thru the list of files to find the required files
for(int i=0; i&lt;allFile.length; i++)
{
    if(allFile[i].getName().matches(".*\\.java")
    {
        resultFile[resultCount] = allFile[i];
        resultCount++;
    }
}</pre>
<p><span id="more-196"></span>In fact, there is a more convenient way to get the above task done. It&#8217;s not hard at all, however people might have often omitted it. We shall make use of &#8220;FilenameFilter&#8221; to filter the files instead of looping.</p>
<pre class="brush:java">// The directory which search would be conducted
File directoryForSearch = new File("C:\folder");

// Using FilenameFilter to do the filtering job
File[] resultFile = directoryForSearch.listFiles(new FilenameFilter()
{
    public boolean accept(File dir, String name)
    {
        // Here we try to match file with extension ".java"
        // Returning YES/TRUE indicate the acceptance of the file else ignore
        return name.matches(".*\\.java");
    }
}</pre>
<p>This code reduction and performance improvement would definitely help when one is trying to implement a complex file search.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cowcomputing.com/2010/03/04/finding-files-with-particular-extension-or-pattern-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
