Solved

How to enumerate File list

Posted on 2006-06-12
12
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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
 
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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
hibernate example issues from command prompt 10 85
sql import cannot be resolved jsp 3 117
wild fly 8 startup error 2 82
netbeans can not run self signed app 8 38
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

751 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