我正在创建一个目录树阅读器

时间:2016-08-27 07:03:27

标签: java

我正在创建一个目录树阅读器并遇到了一个小问题。有人可以帮帮我吗?我的代码如下:

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方法添加到路径中的新变量,以递归方式遍历整个目录。

1 个答案:

答案 0 :(得分:0)

首先,我想你想要将dir列表的值传递给main方法,你可以实现将dirs列表作为String [] args main方法参数传递。 另一个观察是我建议不使用递归,它可以在不需要递归的情况下完成,而是使用某种迭代。 希望这有帮助。