...::: Recent Updates :::...

Sunday, September 4, 2011

Filter File in java (display only a*.java) files

import java.io.FilenameFilter;
import java.io.File;
class filterfilelist implements FilenameFilter
{
String FileName;
String Extention;
String startwith;
String endswith;
String inbetween_startwith,inbetween_endswith;
filterfilelist(String tmp)
{
FileName=tmp.substring(0,(tmp.indexOf('.')));
System.out.println("Name:-"+FileName);
Extention=tmp.substring((tmp.indexOf('.')+1));
System.out.println("Extension:-"+Extention);
}
public boolean accept(File dir,String file)
{
boolean m=true;
String f1=" ";
/* If Star Place At First Place *a.java */
int len=FileName.length()-1;
if(FileName.indexOf('*')==0)
{
startwith=FileName.substring((FileName.indexOf('*')+1));
if(file.indexOf('.')!= -1)
{
f1=file.substring(0,(file.indexOf('.')));
}
m = f1.endsWith(startwith) && file.endsWith(Extention);
}/* a*.java */
if(FileName.indexOf('*')==len)
{
endswith=FileName.substring(0,(FileName.indexOf('*')));
m = file.startsWith(endswith) && file.endsWith(Extention);
}
/* else a*b.java Still remain
{

inbetween_startwith=FileName.substring(0,(FileName.indexOf('*')));
inbetween_endswith=FileName.substring((FileName.indexOf('*')+1));
m =file.startsWith(inbetween_endswith) & file.endsWith(inbetween_startwith) & file.endsWith(Extention);
}*/
return m;
}
}

1 comment:

  1. implementing Above Class filterfilelist

    import java.io.FilenameFilter;
    import java.io.File;
    class testfilterlist
    {
    public static void main(String args[])
    {
    File dir_loc = new File("/home/maulin/Documents/java");
    FilenameFilter fnf=new filterfilelist("Xa*.java");
    File[] type = dir_loc.listFiles(fnf);
    for(File f : type)
    {
    System.out.println("Found File:-\t"+f);
    }
    }
    }

    ReplyDelete