Solved

How to enumerate File list

Posted on 2006-06-12
12
346 Views
Last Modified: 2008-02-01
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
Comment
Question by:_Esam
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16889889
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
 

Author Comment

by:_Esam
ID: 16890116
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
 

Author Comment

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

...
_Esam
0
 

Author Comment

by:_Esam
ID: 16890196
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
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 16890424
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 16890426
you have to pass a file filter to get the files with speific name
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 26

Expert Comment

by:ksivananth
ID: 16890429
oops, sorry again!
0
 
LVL 6

Assisted Solution

by:phuocnh
phuocnh earned 100 total points
ID: 16890543
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
 

Author Comment

by:_Esam
ID: 16894689
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
 

Author Comment

by:_Esam
ID: 16897999
Hi objects,

Can you take a look into this matter?

Thanks...
_Esam.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16898019
are u gettung the list of files without filter?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 16898346
add some debug to see whats happening

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now