read files from a directory ...

Hello experts,

   I have several files such as:

   dog.pet, cat.pet, bird.pet, elephant.pet ...etc., all in the "Animal" folder.
  There are also files called ant.bad, fly.bad ... etc. in the Animal folder as well.

   How do I open the *.pet files, read some info in the file, and then store the filename (eg. dog, cat ...) in to an array ?
   I mean I want an array storing : dog, cat, bird, elephant ... etc.
   And of course, I don't know how many files in Animal are "*.pet" files at very beginning ...

   Is this something possible ? thanks.
LVL 1
meow00Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Just list the directory

File[] petsFiles = new File("Animal").listFiles();
String[] petNames = new String[petsFiles.length];
// Now copy just the name into petNames
meow00Author Commented:
but there are ant.bad & fly.bad in the Animal folder as well ...
And I don't want *.bad files ...

Also, what is the files are in the current folder, how do I call the .listFiles() function ??? thanks.
CEHJCommented:
You can use a FileFilter


File[] petsFiles = new File("Animal").listFiles(new FileFilter() {
    public boolean accept(File f) {
        return f.getName().toLowerCase().endsWith(".pet");
    }
});
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

meow00Author Commented:
I see, thanks...

What do I do if the files are in the current folder (same folder as the main program)

File[] petsFiles = new File("").listFiles();  or
File[] petsFiles = new File().listFiles();
 doesn't seem to work ...:-(
CEHJCommented:
File[] petsFiles = new File(".").listFiles(new FileFilter() {
meow00Author Commented:
thanks so much ... one last question ... how do I copy the name before .pet to the new array ? thanks.
CEHJCommented:
You could do both things at the same time actually:


String[] petNames = null;
List petNamesList = new ArrayList();
File[] petsFiles = new File("Animal").listFiles(new FileFilter() {
    public boolean accept(File f) {
          String name = f.getName();
          String[] parts = name.split("\\.");
          if ("pet".equalsIgnoreCase(parts[parts.length - 1])) {
                petNamesList.add(parts[parts.length - 2]);
                return true;
          }
          else {
                return false;
          }
    }
});
if (petNamesList.size() > 0) {
      petNames = (String[])petNamesList.toArray(new String[petNamesList.size()]);
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
:-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.