Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get a list of files with specified suffix  RSS feed

 
Robin Clark
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I create a File object for a directory and want to get a list of the files in the directory that end with *.ulaw. I know that I can say:

Is there a more elegant way to do this? I was looking at the FilenameFilter interface, but I can't figure out if I can use it in this situation or not.
Thanks for your help!
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Robin Clark:
I was looking at the FilenameFilter interface

The documentation for FilenameFilter defines a single method:

public boolean accept(File dir, String name)
Tests if a specified file should be included in a file list.
Parameters:
dir - the directory in which the file was found.
name - the name of the file.
Returns:
true if and only if the name should be included in the file list; false otherwise.

All you have to do is create a class that implements FilenameFilter and returns true from this method if the name parameter ends in ".ulaw", false otherwise. Pass it in to File.list() call and the array you get back should contain only the desired files.
[ February 09, 2004: Message edited by: Joe Ess ]
 
Robin Clark
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a public class SVVATest that is in a package called vss. I have another public class Filter that is also in the package vss. The Filter.java has been compiled successfully and the Filter.class file is located in the same directory as SVVATest.java. When I try to instantiate a Filter object in my SVVATest class, I get the following error:

I solved this by making the Filter class default instead of public and putting it into the same file as SVVATest, but I think the other way should have worked. I am not having any other troubles compiling the SVVATest.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
Try to compile from outside vss directory (that is your package) like,
javac vss/*.java
that should work even if u've public filter class...
Regards
Maulin
 
Robin Clark
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran into this same problem again and could not fix it this time by making the class internal. So, I played around and determined that it was indeed a classpath problem. I thought that I could fix it by adding the current directory to the classpath (adding '.' to the front of the classpath environment variable) but this did not work and I could not figure out why. Finally I realized that the classpath needed to know the directory that contained the package directory. What it needed to know was the directory one level up. So I hardcoded the entire pathname to the directory that contained the vss directory in my classpath.
How confusing is this. I know I am going to have this problem over and over again. This is the type of problem that is obscured when you use an IDE. I deleted Eclipse last week and I'm using Textpad so that I deepen my understanding of all issues.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Robin
You are right. Using IDE from the first learning lession might create problems later when we have to work on command line tools you know but you are right on the path of command lines
I remember myself when I first learned the "packages". In the book it was written that you should put parent in the classpath and compile packages being outside the package. I have never ever faced this package compilation issue in my life.
Also, I would like to point one more thing about packages- always have the actual directory hierarchy for packages. I have seen some people "not following" that convention and hitting their heads against wall when they want to some strict tools like ANT and all...I mean if we have package why we hesitate to create corresponding directories, afterall that is the "sole" purpose of having packages and grouping our java files etc...
Regards
Maulin
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!