SamW
SamW

Reputation: 109

For loop, getting two successive files

I am using for loop to get files in a file directory. However, I want to take each file and its successive file as the input of my GetDiffs method. How can I achieve so?

Here is my code:

File folder = new File("/Users/Sam/Desktop/Image");
    for (File fileEntry : folder.listFiles()) {
        if (fileEntry.getName().endsWith(".png"))
       Diffs.GetDiffs( , );
    }

Upvotes: 1

Views: 55

Answers (2)

Vishal Patel
Vishal Patel

Reputation: 2970

For geting files in a file directory with your choice extention just some changes you need i thing ERAN also put the ans.

 File f = null;
    for (File fileEntry : myFolderpath.listFiles()) {
       if (getfile.getName().endsWith(".yourfileextention")) {
           if (f != null){
               Diffs.GetDiffs(f, getfile);
        }else{
           f = getfile;
         }
       }      
    }

Upvotes: 0

Eran
Eran

Reputation: 394096

You can use a local variable to store the previous File :

File folder = new File("/Users/Sam/Desktop/Image");
File prev = null;
for (File fileEntry : folder.listFiles()) {
   if (fileEntry.getName().endsWith(".png")) {
       if (prev != null)
           Diffs.GetDiffs(prev, fileEntry);
       prev = fileEntry;
   }      
}

Upvotes: 3

Related Questions