Regular expression or normal search

I have to do the following task:
I have a directory of files, and a text file. The content of text file is like
column 1       column 2
---------------------------
aaag             dhgf
x$5f              %1%f
s%11            dag$
.......
_____________________
what i hv to do is to search all files in the directory, if a filename contains the string in column 1, then replace it to column2 (rename the file).

In this case, should I use regular expression or just loop thru the filenames and use String.indexOf() sth like that?

I am concern with the performance issue, Thanks
nkanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sciuriwareCommented:
As the strings are equal length (4) you might say:
// Create a String[][2] table          with all those name pairs in it.
// int tableCount  is the number of pairs.
File directory = new File("dir");
String[] names = directory.list();
for(int i = 0;  i < names.length();  ++i)
{
     for(int k = 0;  k < tableCount;  ++k)
     {
           int index = names[i].indexOf(table[k][0]);
           if(index >= 0)
           {
                 String newname = names[i].substring(0, index) + table[k][1] + names[i].substring(index + 4);
// I leave it to you to rename the file to its new name ..................
<*>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Using a Properties class will enable you to load those Strings:

Properties props = new Properties();
props.load(getClass().getResourceAsStream(yourFile));
List allSearchStringList = Collections.list(props.propertyNames());

// (for each file)
for(int i = 0;i < allSearchStringList.size();i++) {
      String searchString = (String)allSearchStringList.get(i);
        if (fileNames[index].indexOf(searchString) > -1) {
              // rename it
        }
}
0
CEHJCommented:
Of course, you'd get the replacement String thus:

String replace = props.getProperty(searchString);
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

nkanAuthor Commented:
sorry for confusion...let me clarify the question...i am not care about the properties file method or how to read the text from the .txt file. Instead, i am concerning about how to compare the Files' name with the string from txt file. Since there will be huge amount of files in the directory, so I want effective way to compare string with the filename.

Thanks.
0
sciuriwareCommented:
My code is highly efficient because the list of files is only read once.
The String processing goes in a flash.
<*>
0
CEHJCommented:
>>i am concerning about how to compare the Files' name with the string from txt file

That's what my code shows how to do.
0
nkanAuthor Commented:
...will it be faster if I use regular expression in matching the files?
0
sciuriwareCommented:
Regular expressions are quite powerful and time consuming: slower.
<*>
0
sciuriwareCommented:
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.