title: How to recurrently call function to display parent child relationship between files
body: <p>I have one folder ("all_folders") which contains 5 sub folders ("folder_1","folder_2","folder_3","folder_4" and "folder_5" ).
  Each of these sub-folders contains 2 text files having names like "file_1.txt" ,"file_2.txt" and so on.
  Each of the text file contains address to the next file say "file_1.txt" content is GOTO "file_2.txt".
  In the same manner a file can have multiple address and those file in turn can have address of other files.
  Basically its like a binary tree.I want a user to input a file name for which he wants to know all the address the file he entered contains.
  The output I want should be like a binary tree. I.e like file_10 contains address of file file_7 , file_8 and file_9.
  Again file_9 contains address of file_6 and file_4.
  file_8 contains address of file_5.
  file_7 doesn't contain any address of file and so on....
  I have attached the image of the output which I want and the files and folder i have.</p>
  
  <p>Till now I have written the below code in which i am storing the address which file_10 contains (assuming user entered file_10) in a array list and able to print that.
  But now I want this code to repeat till a file doesn't have any address (see image for the output required).
  I am planning to use JTree to display output as a binary tree as shown in image.
  But that is the second thing and first I need to get the output.</p>
  
  <ol>
  <li>I need help on how we can repeatedly call function to show all file addresses.</li>
  <li>Secondly I am using array list but my concern is , do I need to have as many array list as many level of parent child relationship I have in my tree.
  Because at present I just only have 5 folders and 10 files but it may increase.so there will be lot many array list.</li>
  </ol>
  
  <p>Can you please help me to achieve this output.
  As this is a big code i have tried to write comments wherever possible but sorry as i might not be following good practices in the code as I am a beginner.</p>
  
  <p>Output Image:
  <a href="https://i.stack.imgur.com/ys4Sl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ys4Sl.png" alt="Output Required"></a></p>
  
  <p>Attached all_folder files:
  <a href="https://drive.google.com/open?id=0B9hvL6YZBpoTRkVYV0dUWEU5V2M" rel="nofollow noreferrer">https://drive.google.com/open?id=0B9hvL6YZBpoTRkVYV0dUWEU5V2M</a></p>
  
  <p>My Code is as below:</p>
  
  <pre><code>import java.io.File;
  import java.io.FileNotFoundException;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.Scanner;
  
  public class FindFile
  {
      String result;
      static ArrayList&lt;String&gt; storeAllFileName = new ArrayList&lt;String&gt;(); // This array list will store all file names from all the sub-folders of all_folders
      static int i = 0;
  
      public void listFilesAndFilesSubDirectories(String directoryName)
          {
              File directory = new File(directoryName);
              File[] fList = directory.listFiles();
              for (File file : fList)
                  {
                      if (file.isFile())
                          {
                              if (file.getName().endsWith(".txt")) // Checking if the file is
                                                                                                          // a text file
                                  {
                                      storeAllFileName.add(file.getName().toLowerCase());
                                      i++;
                                  }
                          } else if (file.isDirectory())
                          {
                              listFilesAndFilesSubDirectories(file.getAbsolutePath());
                          }
                  }
          }
  
      public static void main(String[] args) throws FileNotFoundException
          {
              recurrenceFileFind();
          }
  
      public static void recurrenceFileFind() throws FileNotFoundException
          {
              FindFile FindFile = new FindFile();
  
              String fileName = "file_10.txt"; // Hardcoded this value assuming user
                                                                                  // have entered file_10.txt
              final String directoryName = "C:\\all_folders"; // Hardcoded this value
                                                                                                              // assuming all folder
                                                                                                              // of user are placed in
                                                                                                              // C:\all_folders
                                                                                                              // directory
  
              FindFile.listFilesAndFilesSubDirectories(directoryName);
              FindFile.searchDirectory(new File(directoryName), fileName);
  
              System.out.println("\nFile Found at: " + FindFile.getResult());
              String filedirectoryName = FindFile.getResult(); // Passing the location
                                                                                                                  // of the file found
                                                                                                                  // at so that now we
                                                                                                                  // can read the text
                                                                                                                  // of the file and
                                                                                                                  // search for the
                                                                                                                  // address of child
                                                                                                                  // files
  
              File file = new File(filedirectoryName);
              Scanner in = new Scanner(file);
  
              ArrayList&lt;String&gt; viewText = new ArrayList&lt;String&gt;(); // This array list
                                                                                                                          // will store the
                                                                                                                          // content of the
                                                                                                                          // file
  
              while (in.hasNext())
                  {
                      viewText.add(in.next().toLowerCase()); // Store the content of file
                                                                                                      // in a array list viewText
                  }
  
              ArrayList&lt;String&gt; comparingList = new ArrayList&lt;String&gt;(viewText); // copy
                                                                                                                                                      // viewText
                                                                                                                                                      // array
                                                                                                                                                      // List
                                                                                                                                                      // to
                                                                                                                                                      // new
                                                                                                                                                      // array
                                                                                                                                                      // list
                                                                                                                                                      // comparingList
  
              comparingList.retainAll(storeAllFileName); // store only those address
                                                                                                      // in the comparingList for
                                                                                                      // which we have file with
                                                                                                      // that name in any of the
                                                                                                      // sub-folder, as the file
                                                                                                      // can have extra content
                                                                                                      // like GOTO or any other
                                                                                                      // words
  
              System.out.println("\n\"" + file.getName() + "\"" + " contains below files:");
  
              allListPrint(comparingList); // printing address of files which the
                                                                          // parent file contains
  
          }
  
      public void searchDirectory(File directory, String fileNameToSearch)
          {
  
              if (directory.isDirectory())
                  {
                      search(directory, fileNameToSearch);
                  } else
                  {
                      System.out.println(directory.getAbsoluteFile() + " is not a directory!");
                  }
  
          }
  
      private void search(File directory, String fileNameToSearch)
  
          {
              if (directory.isDirectory())
                  {
                      System.out.println("Searching directory ... " + directory.getAbsoluteFile());
                      if (directory.canRead())
                          {
                              for (File temp : directory.listFiles())
                                  {
                                      if (temp.isDirectory())
                                          {
                                              search(temp, fileNameToSearch);
                                          } else
                                          {
                                              if (fileNameToSearch.equalsIgnoreCase(temp.getName().toLowerCase()))
                                                  {
                                                      result = (temp.getAbsoluteFile().toString());
                                                  }
  
                                          }
                                  }
  
                          } else
                          {
                              System.out.println(directory.getAbsoluteFile() + "Permission Denied");
                          }
                  }
  
          }
  
      private static void allListPrint(ArrayList&lt;String&gt; List) // method to print
                                                                                                                          // array list
          {
              Iterator&lt;String&gt; itr = List.iterator();
              while (itr.hasNext())
                  {
                      System.out.println(itr.next());
                  }
  
          }
  
      public String getResult()
          {
              return result;
          }
  
  }
  </code></pre>