Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a file related question

Status
Not open for further replies.

skarosi

Programmer
Jan 25, 2004
140
GR
Hi all, I am sure that someone else has already asked something like that, but i am not sure how i should search for it. it seems too complicated for a serach string.
anyhow, here it is:
i want to open all files with a specific extension from the folder i am working on. for example i have 3 txt files, i want to open them one by one.
the problem is i dont know the names of the files, just the extensions, that they are ".txt". i found a method in the API called listFiles(Filter) but i didnt figure out how it works.
also, i want to open these within a loop, so a static way of entering the file name as a string will not be a good idea.
can anybody help?
thanks, Ilias
 
You need to implement a FileFilter :

Code:
class MyFileFilter implements FileFilter {
   public boolean accept(File f) {
      if (f.getName().endsWith(".txt")) {
         return true;
      } else {
         return false;
      }
   }
}

and then you can use the listFiles() method you mention :

Code:
File dir = new File("/mydir");
File[] files = dir.listFiles(new MyFileFilter());
/// and loop the File array

--------------------------------------------------
Free Database Connection Pooling Software
 
... and sedj's filter code could be made more succinct if you like :-
Code:
class MyFileFilter implements FileFilter {
   public boolean accept(File f) {
      return f.getName().endsWith(".txt");
   }
}
... it's a matter of personal-preference and whether you like typing [smile].

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
thanks for the help, but i am geting a strange null pointer exception whenever i am trying to use the file array.
it is like a normal array, right?
i have used both of these lines:
if (files[0]!=null)

for (int i=0;i<files.length;i++)
and i am getting a "null pointer exception" for both of them.
what is the problem?
cheers
 
API Doc said:
for File.listFile(FileFilter filter) ...
Returns:
An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

So I'd guess that the abstract pathname does not denote a directory ... assuming you're not getting an I/O error.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
no, i am getting a nullPOinterException.
i think that the problem might b on the directory i look in.
how do i set it to look on the directory the java file is on?
cheers
 
I think you're getting a NullPointerException because the listFile() method is returning a null as indicated by the API snippet I posted above. If you just take the array returned from this method and try to access it, if it is null, you will get that exception. You should test that it isn't null before accessing it.

To set it to look at the directory, you create the File object using the path to the directory as a parameter in the constructor.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
ok, i think i got it working
thanks ppl!
oh, by the way, how do u put the "code" box on the posts?
thanks again
 
... click on the 'Process TGML' link just above the 'submit post' button to find out about code tags and much more...

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
sorry to bother u again m8s, but how can i delete one of those files that it found?
it is in file[] format and it makes it a bit tricky
thanks
 
yea, never mind. i figured it out. there was a problem with the directories.
cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top