2016-05-11 6 views
1

5つのサブフォルダ(「folder_1」、「folder_2」、「folder_3」、「folder_4」および「folder_5」)を含む1つのフォルダ(「all_folders」)があります。 。 これらの各サブフォルダには、「file_1.txt」、「file_2.txt」などの名前を持つ2つのテキストファイルが含まれています。 各テキストファイルには、次のファイルへのアドレスが含まれています。「file_1.txt」の内容はGOTO「file_2.txt」です。 同様に、ファイルは複数のアドレスを持つことができ、それらのファイルは他のファイルのアドレスを持つことができます。 基本的にバイナリツリーのようなものです。ユーザが入力したファイルにすべてのアドレスを知りたいファイル名を入力したいとします。 私の出力はバイナリツリーのようになります。私はfile_10のようにファイルfile_7、file_8とfile_9のアドレスを含んでいます。 また、file_9にはfile_6とfile_4のアドレスが含まれています。 file_8にはfile_5のアドレスが含まれています。 file_7にはファイルのアドレスなどが含まれていません.... 私が望む出力と私が持っているファイルとフォルダのイメージを添付しました。ファイル間の親子関係を表示する関数を再帰的に呼び出す方法

これまで、file_10に含まれるアドレス(ユーザーがfile_10を入力したと仮定します)を配列リストに格納し、そのアドレスを印刷できる次のコードを書いています。 しかし、このコードは、ファイルにアドレスがないときまで繰り返す必要があります(必要な出力については画像を参照)。 JTreeを使用して、画像をバイナリツリーとして表示する予定です。 しかしそれは第2の事であり、まず出力を得る必要があります。

  1. 関数を繰り返し呼び出してすべてのファイルアドレスを表示する方法については、私は助けが必要です。
  2. 2番目に私は配列リストを使用していますが、懸念しているのは、私がツリー内に持っている多くの親子関係の配列リストを必要としますか? 現在のところ、私は5つのフォルダと10のファイルしか持っていませんが、多くの配列リストが増えます。

この出力を達成するために手伝ってください。 これは大きなコードなので、私は可能な限りコメントを書こうとしましたが、私が初心者であるのでコード内の良い習慣に従わないかもしれないので、残念です。

出力イメージ: Output Required

all_folder添付ファイル: https://drive.google.com/open?id=0B9hvL6YZBpoTRkVYV0dUWEU5V2M

マイコードは以下の通りです:

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<String> storeAllFileName = new ArrayList<String>(); // 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<String> viewText = new ArrayList<String>(); // 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<String> comparingList = new ArrayList<String>(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<String> List) // method to print 
                                 // array list 
     { 
      Iterator<String> itr = List.iterator(); 
      while (itr.hasNext()) 
       { 
        System.out.println(itr.next()); 
       } 

     } 

    public String getResult() 
     { 
      return result; 
     } 

} 
+0

本当にバイナリツリーですか?あなたがここに持っている木はバイナリではないからです。ツリーがバイナリの場合、各ファイルは最大で2つの他のファイルを指すことができます。 – MAZDAK

+0

ええ、それは単純な木と考えることができます、それは任意の組み合わせを持つことができます。 – Rahul

+0

私はまだ実装できない答えを探しています:( – Rahul

答えて

0

は、ここで再帰的なソリューションです。私はあなた自身がファイルのディレクトリからHashMap<String,Node>を作成できると仮定します。私は手動で時間を節約するためにそのようなHashMapを作成しました。しかし、自動的に行うのは簡単です。あるパスではすべてのファイルを読み込み、ファイルごとにNodeを作成し、2回目のパスではchildrenフィールドを更新します。

class Node { 

String name; 
List<Node> children = new ArrayList(); 

public Node(String name) { 
    this.name = name; 
    } 
} 

public class FileTree { 

//recursive function for returning children 
public void retChildHeirarchy(Node n) { 

    if (n == null) { 
     return; 
    } 

    for (Node child : n.children) { 
     retChildHeirarchy(child); 
     System.out.println(child.name); 
    } 
} 

public static void main(String[] args) { 

    HashMap<String, Node> treeStructure = new HashMap<>(); 

    /*To save time, I manually create the nodes and update HashMap of Nodes 
    but you can do it automatically. 
    */ 
    Node f4 = new Node("file_4"); 
    Node f6 = new Node("file_6"); 
    Node f7 = new Node("file_7"); 
    Node f8 = new Node("file_8"); 
    Node f9 = new Node("file_9"); 
    Node f10 = new Node("file_10"); 

    //update f_10 
    f10.children.add(f9); 
    f10.children.add(f8); 
    f10.children.add(f7); 
    //update f9 
    f9.children.add(f6); 
    f9.children.add(f4); 

    treeStructure.put("file_4", f4); 
    treeStructure.put("file_6", f6); 
    treeStructure.put("file_7", f7); 
    treeStructure.put("file_8", f8); 
    treeStructure.put("file_9", f9); 
    treeStructure.put("file_10", f10); 

    FileTree ft = new FileTree(); 
    //call the recursive function for the Node that you want: 
    ft.retChildHeirarchy(f9); 
    } 
} 

出力は次のとおりです。 f10の場合、再帰関数は正常に機能しますが、手動で更新する場合は、f10、子のリストに5,2,3、および1を追加しませんでした。

ft.retChildHeirarchy(f9); 

    file_6 
    file_4 

ft.retChildHeirarchy(f10); 

    file_6 
    file_4 
    file_9 
    file_8 
    file_7 
関連する問題