Link to home
Start Free TrialLog in
Avatar of illumin
illumin

asked on

Adding distinct filenames to a string array

Hi there
I have a folder which i want to add the contents of it to a string array. However some of the files have the same name but a different file extension eg foo.txt and foo.zip
I have being using
File fileList = new File(path);
String[] projects = fileList.list();

but it is no use anymore as it shows two files instead of one. I just want one entry of each  distinct filename in the string array. Ive being trying to use a filefilter but to na avail. Your help is appriciated.
 
Avatar of schybert
schybert

I'm confused... you don't want to show all the files in the directory? foo.zip and foo.txt are after all distinct file names...
Avatar of illumin

ASKER

Ok ill try to explain it better... I have two files in a dir called foo.txt and foo.zip .I only want to add one foo to the string array instead of two. The code that ive come up with so far still shows up two foo's :(

package mypackage;
import java.io.*;

public class ZipFilter implements FilenameFilter
{  
  public boolean accept(File dir,String name)
   {  
    return(name.endsWith(".zip"));
   }
}

String path = (String)session.getAttribute("Path");
  File fileList = new File(path);
  ZipFilter filter = new ZipFilter();
  fileList.list(filter);
Avatar of illumin

ASKER

String[] projects = fileList.list(); should be on the end of the las comment
The second block of code is in a different class :P
Avatar of illumin

ASKER

Sorry i am so stupid i was doing this
String[] projects = fileList.list();

instead of this

 String[] projects = fileList.list(filter);

It works fine for me now ...
hehe, great
Avatar of illumin

ASKER

Can any mods delete this question please
illumin:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial