Link to home
Start Free TrialLog in
Avatar of _Esam
_Esam

asked on

How to enumerate File list

Hi,

In my SourceCP class:
I have say three methods:

void initialize();
File getCurrentFile();
File getNextFile();


In my initialize() method:
I get the file directory and list the files under that directory.
I want to get the files that has certain names such as files that has the subString FEEDBACK

In my getCurrentFile method:
I want to get the first file in the list under the directory - as I found in initialize()

In my getNexttFile method:
I want to get the next file in the list under the directory - as I found in initialize()


In my Source class:
I want to get the current file ... then the next file.. and thus process them...

How do I go about coding the above methods?
I tried certain ways - didn't work...

Here is what I tried:

public class SourceCP{

   File currentFile = null;
   File filePath = null;
   String subString = null;
   String[] fileNames = null;
   int totalFiles = 0;
   int currentFileIndex = 0;

public void initialize(){
   filePath = new File(ConfigParameters.getInstance().getDataFilePath());  //from XML - works ok
   subString = ConfigParameters.getInstance().getFileNameSubString(); //from XML - works ok - to get  
                                                                                            // type of files I want to work with within this directory

  Now  I want to list the files within this directory that has the fileNameSubString String.
  I want to assign the currentFIle to the first index within this list - such that in my getNextFile I can increment it.
  The method getNextFile illustrates what I really want to do...
  Here I want to store all the file names in an array.
  Use an index to assign the first file in the array in currentFile.
  get a total of the files.

}


public File getCurrentFile(){

return currentFile;
}


public  File getNextFile()
          {
        File lReturnFile = null;

       if (++currentFileIndex < totalFiles)
        {
            currentFile =  new File(ConfigParameters.getInstance().getDataFilePath() +
                                    fileNames[currentFileIndex]);
        }
        else
        {
            currentFile = null;
        }
   
        return currentFile;
    }


}

Let me know how to go about this!!
I gave full points since it needs some real contribution than some mere links :)

Let me know if you need further clarificaiton...


Thanks..
_Esam.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

this shows you how to get the file list

http://javaalmanac.com/egs/java.io/GetFiles.html

Easiest would be to store the File list as a member variable
and also the index of current

private File[] files;
private int index;

then to get current file

return files[index];

and to getNextFile just increment the index

index++;
return currentFile();


you'll aqlso need to add some code to getCurrentFile() to check that you haven't gone pastthe last file in the list
ie. that index < files.length
Avatar of _Esam
_Esam

ASKER

Ok, I got the currentFile and getNext file working -- just with simple listing as File[] fileList = dir.listFiles();

Now how do I make sure that I only get the files with names as FEEDBACK ... from the lists as:

FEEDBACK20060601.feedback
FEEDBACK20060602.feedback
FEEDBACK20060603.feedback
FEED20060601.feedback
FEED20060602.feedback
FEED20060603.feedback
FEEDBACK20060601.fbk
FEEDBACK20060602.fbk
FEEDBACK20060603.fbk

I just want to get the first three files!!!

Thanks...
_Esam...
Avatar of _Esam

ASKER

FEEDBACK comes from a value (String) configured by XML such that it can be changed...

...
_Esam
Avatar of _Esam

ASKER

How do I filter the File[] , the file array??

Im I contrained to using String[] with file names and then using the Filter and then constructing files with path + name again ???

Got a little confused here?

Thanks.
_Esam.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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
you have to pass a file filter to get the files with speific name
oops, sorry again!
SOLUTION
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
Avatar of _Esam

ASKER

Nope, it's not working with the FileFilter ....
Something is missing that is being ignored...

I have configured the file path as resources/feed/data/       in the XML
It is relative to my current working project directory.

If I do a SOP for the path, I get it as:   resources\feed\data

And under this directory I have those data files that I want to filter...

I am not getting anything back from the filter .. I tried it as:


I have as:
File[] list = null; //member variable

File path = null; //local method variable

path = the_path;  // if I print it - it gives -  resources\feed\data   as I said earlier
subString = "FEEDBACK";
       fileFilter = new FileFilter() {
           public boolean accept(File file) {
               return file.getPath().indexOf(subString)>=0;
           }
       };

list = path.listFiles(fileFilter);

What's wrong here?
I didn't get anything back...

Let me know..
Thanks.
_Esam
Avatar of _Esam

ASKER

Hi objects,

Can you take a look into this matter?

Thanks...
_Esam.
are u gettung the list of files without filter?
SOLUTION
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