3
import java.util.*;
import java.io.*;
public final class FileListing2 
{
    public static void main(String... aArgs) throws FileNotFoundException 
    {
         File startingDirectory= new File(aArgs[0]);
         List<File> files = FileListing.getFileListing(startingDirectory);
         //print out all file names, in the the order of File.compareTo()
         for(File file : files )
         {
              System.out.println(file);
         }
    }
    static public List<File> getFileListing(File aStartingDir) throws FileNotFoundException 
    {
        validateDirectory(aStartingDir);
        List<File> result = getFileListingNoSort(aStartingDir);
        Collections.sort(result);
        return result;
    }
    static private List<File> getFileListingNoSort(File aStartingDir) throws FileNotFoundException 
    {
          List<File> result = new ArrayList<File>();
          File[] filesAndDirs = aStartingDir.listFiles();
          List<File> filesDirs = Arrays.asList(filesAndDirs);
          for(File file : filesDirs) 
          {
            result.add(file); //always add, even if directory
            if ( ! file.isFile() ) 
            {
                   //must be a directory
                   //recursive call!
                   List<File> deeperList = getFileListingNoSort(file);
                   result.addAll(deeperList);
            }
            return result;
          }

       }
       static private void validateDirectory (File aDirectory) throws FileNotFoundException 
       {
          if (aDirectory == null) 
          {
                 throw new IllegalArgumentException("Directory should not be null.");
          }
          if (!aDirectory.exists()) 
          {
                 throw new FileNotFoundException("Directory does not exist: " + aDirectory);
          }
          if (!aDirectory.isDirectory()) 
          {
                 throw new IllegalArgumentException("Is not a directory: " + aDirectory);
          }
          if (!aDirectory.canRead()) 
          {
                 throw new IllegalArgumentException("Directory cannot be read: " + aDirectory);
           }
        }
      }

我从这个站点复制了这段代码,并试图使其适应我们的环境。不幸的是,我无法编译 $%!@#*($ 东西。我在 main 方法中不断收到错误:错误:重复修饰符。

public static void main(String... aArgs) throws FileNotFoundException是标记为错误的行。

我在这里看不到任何重复的修饰符,我所有的花括号和括号似乎都在正确的位置,我完全被难住了。

这是我第一次使用可变参数...我不确定这是否给我带来了问题?我扫描了 Java 文档,但没有发现任何危险信号。此外,当我更改String...String[]. 我觉得我可能会得到一些空值,所以我宁愿留String...在方法中。

有人看到我遗漏的任何东西吗?我觉得我在春季脑筋急转弯中看着那个巴黎......

4

2 回答 2

7

在方法getFileListingNoSort()中,行在return result;方法之外,将其向上移动一行以将其放入它所属的位置。

于 2012-04-23T19:02:05.350 回答
1

我无法重现您重复的修饰符错误(使用 JDK 1.7.0),但方法的返回语句getFileListingNoSort不在方法体中。

请格式化您的代码,使其有意义,您将看到此类问题。以下编译良好:

import java.util.*;
import java.io.*;

public final class FileListing2 {
    public static void main( String... aArgs ) throws FileNotFoundException {
        File startingDirectory = new File( aArgs[0] );
        List<File> files = FileListing2.getFileListing( startingDirectory );
        for(File file : files) {
            System.out.println( file );
        }
    }


    static public List<File> getFileListing( File aStartingDir ) throws FileNotFoundException {
        validateDirectory( aStartingDir );
        List<File> result = getFileListingNoSort( aStartingDir );
        Collections.sort( result );
        return result;
    }


    static private List<File> getFileListingNoSort( File aStartingDir ) throws FileNotFoundException {
        List<File> result = new ArrayList<File>();
        File[] filesAndDirs = aStartingDir.listFiles();
        List<File> filesDirs = Arrays.asList( filesAndDirs );
        for(File file : filesDirs) {
            result.add( file ); // always add, even if
                                        // directory
            if(!file.isFile()) {
                List<File> deeperList = getFileListingNoSort( file );
                result.addAll( deeperList );
            }
        }
        return result;
    }


    static private void validateDirectory( File aDirectory ) throws FileNotFoundException {
        if(aDirectory == null) {
            throw new IllegalArgumentException( "Directory should not be null." );
        }
        if(!aDirectory.exists()) {
            throw new FileNotFoundException( "Directory does not exist: " + aDirectory );
        }
        if(!aDirectory.isDirectory()) {
            throw new IllegalArgumentException( "Is not a directory: " + aDirectory );
        }
        if(!aDirectory.canRead()) {
            throw new IllegalArgumentException( "Directory cannot be read: " + aDirectory );
        }
    }
}
于 2012-04-23T19:05:04.747 回答