1

When I compile I don't get any errors but the ArrayList<File> from the method is not getting into the main.

All the entries got added into the Folders ArrayList but it doesn't get passed into the main method.

public static ArrayList<File> Process(File aFile)
 {
  ArrayList<File> Folders = new ArrayList<File>();

  if(aFile.isFile())
  {
     String filenom = aFile.getPath();
     if (filenom.toLowerCase().endsWith(".java")){
        Folders.add(aFile);
     }
  }
  else if (aFile.isDirectory()) {
     File[] listOfFiles = aFile.listFiles();
     if(listOfFiles!=null) {
        for (int i = 0; i < listOfFiles.length; i++)
           Process(listOfFiles[i]);
     } 
     else {
        System.out.println(" [ACCESS DENIED]");
     }
  }    
  return (Folders);
}

public static void main(String[] args) throws IOException
{
  ArrayList<File> FAddress = new ArrayList<File>();
  File dir = new File("C:/");
  FAddress = Process(dir);
     if (FAddress.isEmpty())
     System.out.println("WTF?!");
  else{
     for (File fl : FAddress) {
        String FileAddress = fl.getName();
        System.out.println(FileAddress);
     }
  }
}

3 Answers 3

2

The problem is with your recursion - you correctly identify if a File object is a directory, and call Process on it:

for (int i = 0; i < listOfFiles.length; i++)
    Process(listOfFiles[i]);

But you forgot to add its result to the return value (Folders), so any result from subdirectories won't be added to the return value:

for (int i = 0; i < listOfFiles.length; i++)
    Folders.addAll(Process(listOfFiles[i]));
Sign up to request clarification or add additional context in comments.

Comments

1

You create

ArrayList<File> Folders = new ArrayList<File>();

in every time when run Process. This function have recursion, so because you start at C:/ folder you never update this array.

for correct processing you should use something like:

public static ArrayList<File> Process(File aFile) {
    ArrayList<File> Folders = new ArrayList<File>();
    Process(aFile, Folders);
    return folders;
}

private static void Process(File aFile, ArrayList<File> Folders) {
    ... //your implementation without defining Folders and returning it
}

this will be usefull if you need process many levels of tree for files.

Comments

1

You can define a class level variable static ArrayList<File> Folders = new ArrayList<File>(); and then use the same for populating files.

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class test {
    static ArrayList<File> Folders = new ArrayList<File>();
    public static void Process(File aFile) {


        if (aFile.isFile()) {
            String filenom = aFile.getPath();
            if (filenom.toLowerCase().endsWith(".log")) {
                Folders.add(aFile);
            }
        } else if (aFile.isDirectory()) {
            File[] listOfFiles = aFile.listFiles();
            if (listOfFiles != null) {
                for (int i = 0; i < listOfFiles.length; i++)
                    Process(listOfFiles[i]);
            } else {
                System.out.println(" [ACCESS DENIED]");
            }
        }
    }

    public static void main(String[] args) throws IOException {
        File dir = new File("C:/ukba/");
        Process(dir);
        if (Folders.isEmpty())
            System.out.println("WTF?!");
        else {
            for (File fl : Folders) {
                String FileAddress = fl.getName();
                System.out.println(FileAddress);
            }
        }
        Folders = null;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.