2016-08-27 4 views
0

Я создаю читатель дерева каталогов и столкнулся с небольшой проблемой. Может ли кто-нибудь помочь мне, пожалуйста? Мой код выглядит следующим образом:Я создаю читатель дерева каталогов

import java.io.File; 

public class DirectoryTree { 

    //dirlist method 
    private static void dirlist (String fname) 
    { 
     File  dir = new File (fname); 
     String[] chld = dir.list (); 
     String[] path = {""}; 
     int nDir = 0; 

     //Break the recursion 
     if (chld == null) 
     { 
      System.out.println ("Specified directory does not exist or is not a directory."); 
      System.exit (0); 
     } 
     else 
     { 
      //Iteration 
      for (int i = 0; i < chld.length; i++) 
      { 
       String fileName = chld[ i ]; 
       System.out.println (fileName); 

       if (dir.isDirectory()) 
       { 
        nDir = nDir++; 
        path[i] = dir.getPath(); 
       } 
      } 
      int nFile = (chld.length) - nDir; 

      //Display out to user 
      System.out.println ("The amount of directories in current directory is: " + nDir + " The amount of files in current directory is " + nFile); 
     } 
    } 

    //Get method to access dirlist method for recursion 
    public getDirlist() 
    { 
     return dirlist(); 
    } 

    //Main 
    public static void main (String[] args) 
    { 
     //Recursion of dirlist method 
     for (int i = 0; i < path.length; i++) //<--- this is the error path not found 
     { 
      DirectoryTree dirPath = new DirectoryTree(.getDirlist()); 
      dirPath dirFind = new dirPath(path[i]); 
      dirFind.(dirPath()); 
     } 

     //Deal with errors 
     switch (args.length) 
     { 
      case 0: 
       System.out.println ("Directory was not mentioned."); 
       System.exit (0); 
      case 1: 
       dirlist (args[ 0 ]); 
       System.exit (0); 
      default: 
       System.out.println ("Multiple directorys are not allow."); 
       System.exit (0); 
     } 
    } 
} 

Я хочу, чтобы иметь возможность читать в новой переменной добавляется в пути от DirList метода рекурсивно пройти через весь каталог.

+5

Ну ... И в чем проблема? – Seelenvirtuose

+1

У вас должно получиться лучшее название, чем это. – usr2564301

ответ

0

Во-первых, я предполагаю, что вы хотите передать значение каталога dir в основной метод, вы можете добиться того, чтобы передать список dirs в качестве аргумента основного аргумента String [] args. Другое замечание состоит в том, что я рекомендую не использовать рекурсию, это можно сделать без необходимости рекурсии, вместо этого использовать какую-то итерацию. Надеюсь, что эта помощь.