[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Regular expression or normal search

Posted on 2004-11-11
12
Medium Priority
?
161 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:nkan
  • 4
  • 3
  • 2
9 Comments
 
LVL 24

Accepted Solution

by:
sciuriware earned 100 total points
ID: 12564133
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 12564478
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12564493
Of course, you'd get the replacement String thus:

String replace = props.getProperty(searchString);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:nkan
ID: 12571760
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 12572684
My code is highly efficient because the list of files is only read once.
The String processing goes in a flash.
<*>
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12573440
>>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
 

Author Comment

by:nkan
ID: 12581480
...will it be faster if I use regular expression in matching the files?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12581681
Regular expressions are quite powerful and time consuming: slower.
<*>
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12861057
Thanks.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month17 days, 15 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question