0

Please see the below code ,basically i am checking some specific word in filename to determine the type of file.Please see the below code and leet me know if this is the best way otherwise please suggest.

Example File Name : incoming_EMP_data.dat
final String[] files = file.list();
if (files != null && files.length > 0) {                
fileName = files[0];
if(files[0].replace("_"," ").indexOf("EMP")!=-1)
System.out.println("EMP file");
else
System.out.println("NOT EMP file");
}

if file name has incoming_TEMP_data.dat also above code will pass,I am looking exact word ,

Regards,

mkr

user1476092
  • 63
  • 1
  • 2
  • 9

2 Answers2

0

The filename has a pattern? I mean, will always come like "" ?

If yes, you can change your code to:

    if(files[0].indexOf("_EMP_") != -1)

Regards.

dansouza
  • 111
  • 5
0

Can for example be done with regular expressions and the word boundary matcher \b:

String[] examples = {
        "incoming_EMP_data.dat",
        "EMP.pemp", // word boundary can be beginning or end
        "Hurz-EMP#poof", // many 
        "~EMP=", // many strange characters are boundaries
        "XYZ_TEMP.dat",
        "1EMP2", // numbers are considered word
        "ßEMPö" // unicode characters are also considered word
};

Pattern pattern = Pattern.compile("\\bEMP\\b");
// word boundary EMP word boundary
// _ counts as part of a word though

for (String string : examples) {
    if(pattern.matcher(string.replace("_", " ")).find()) {
        System.out.println(string + " is EMP");
    } else {
        System.out.println(string + " is not.");
    }
}

prints

incoming_EMP_data.dat is EMP
EMP.pemp is EMP
Hurz-EMP#poof is EMP
~EMP= is EMP
XYZ_TEMP.dat is not.
1EMP2 is not.
ßEMPö is not.

The nice thing about \b is that it matches even when the word boundary is the beginning or end of the string. That's something not easily solved otherwise. _ is unfortunately considered part of a word so you will either have to replace it (as done above) or extend the pattern to include it as alternative "(\\b|_)EMP(\\b|_)".

zapl
  • 63,179
  • 10
  • 123
  • 154