[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

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.
0
_Esam
Asked:
_Esam
  • 5
  • 3
  • 3
  • +1
3 Solutions
 
objectsCommented:
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
0
 
_EsamAuthor Commented:
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...
0
 
_EsamAuthor Commented:
FEEDBACK comes from a value (String) configured by XML such that it can be changed...

...
_Esam
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
_EsamAuthor Commented:
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.
0
 
objectsCommented:
use a filter when u call listFiles()

   FileFilter fileFilter = new FileFilter() {
        public boolean accept(File file) {
            return file.getPath().indexOf(subString)>=0;
        }
    };
    files = dir.listFiles(fileFilter);
0
 
ksivananthCommented:
you have to pass a file filter to get the files with speific name
0
 
ksivananthCommented:
oops, sorry again!
0
 
phuocnhCommented:
Hi _Esam!
You must implement java.io.FilenameFilter
public class MyFilter implement FilenameFilter
{
private String beginning;
 public MyFilter(String beginning)
{
  this.beginning=beginning
}
 public boolean accept(File dir, String name)
{
 return name.toUpperCase().startsWith(beginning);
}
}
now as objects have recommended:
private File[] filelist = File.list(new MyFilter("FEEDBACK"));
private int currentIdx = 0;
public File getCurrent()
{
 if (currentIdx < filelist.length)  return filelist[currentIdx];
else
return null;
}
public File getNextFile()
{
 currentIdx ++;
return getCurrent();
}
}
Phuoc
0
 
_EsamAuthor Commented:
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
0
 
_EsamAuthor Commented:
Hi objects,

Can you take a look into this matter?

Thanks...
_Esam.
0
 
ksivananthCommented:
are u gettung the list of files without filter?
0
 
objectsCommented:
add some debug to see whats happening

               System.out.println(substring+":"+file.getPath());
               return file.getPath().indexOf(subString)>=0;
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now