Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

regex and implementing FilenameFilter problem

807603May 10 2006 — edited Dec 6 2007
Hello,

So what I'm trying to do is to create a program that takes a certain set of files, pulls the first line of each file and uses it to name the file. Right now, I'm at the point of getting a listing of files based on a patterns. So when I run the program on the command line (of a windows machine), it spit out the files that I'm looking for. Something like:

java FileRenamer *.txt

Above should produce a listing of only files that have .txt on them (I want to have the capability to choose *.txt or whatever other combination of pattern match).

To do the above, I want to use a FileNameFilter interface to figure out what files match. The problem that I'm running into is that when I run a unit test against the getFilesListBasedOnPattern method, I get:

java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
*.txt

The problem is that the *.txt has a regex character (the *) and I'm not sure how make it behave like the wildcard in the dos command line where *.txt means everything that has .txt at the end.

The code listing is below. Does anyone have any suggestions on how to best approach this?

mapsmaps

=======> Code below <=======
// unit test snippet that causes blow out:
FileRenamer fr = new FileRenamer();

String [] strArrFilesBasePattern = fr.getFilesListBasedOnPattern(dirTestFiles,"*.txt");

====
//main program
package com.foo.filerenamer;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 
 * 
 *
 * TODO Use regexp to filter out input to *.txt type of thing or nothing else
 */
public class FileRenamer
{
    // Vallid file patterns are *.*, ?
    public static final String strVALIDINPUTCHARS = "[_.a-zA-Z0-9\\s\\*\\?-]+";
    private static Pattern regexPattern = Pattern.compile(strVALIDINPUTCHARS);
    private static Matcher regexMatcher;
    
    /**
     * @param args
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws InterruptedException
    {
        int intMillis = 0;
        if (args.length > 0)
        {
            try
            {
                intMillis = Integer.parseInt(args[0]);
                System.out.println("Sleep set to " + intMillis + " seconds"); 
            }
            catch (NumberFormatException e) 
            {
                intMillis = 5000;
                System.out.println("Sleep set to default of " + intMillis + " since first parameter was non-int");
            }
            for (int i=0;i<args.length;i++)
            {
                System.out.println("hello there - args["+i+"] = "+ args);
Thread.sleep(intMillis);
}
}
// TODO Auto-generated method stub

}

public boolean checkArgs(String [] p_strAr)
{
boolean bRet = false;



if (p_strAr.length != 1)
{
return false;
}
else
{
regexMatcher = regexPattern.matcher(p_strAr[0]);
bRet = regexMatcher.matches();
}

return bRet;
}


public String[] getFilesListBasedOnPattern(File p_dirFilesLoc, String p_strValidPattern)
{
String[] strArrFilteredFileNames = p_dirFilesLoc.list(new RegExpFileFilter(p_strValidPattern));

return strArrFilteredFileNames;
}
}


class RegExpFileFilter implements FilenameFilter
{
private String m_strPattern = null;
private Pattern m_regexPattern;


public RegExpFileFilter(String p_strPattern)
{
m_strPattern = p_strPattern;
m_regexPattern = Pattern.compile(m_strPattern);
}

public boolean accept(File m_directory, String m_filename)
{
if (m_regexPattern.matcher(m_filename).matches())
return true;

return false;

}

}

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 3 2008
Added on May 10 2006
8 comments
134 views