Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to enumerate File list

Posted on 2006-06-12
12
Medium Priority
?
354 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1600 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
 
LVL 26

Expert Comment

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

Assisted Solution

by:phuocnh
phuocnh earned 400 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 1600 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

926 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