编译时为什么会出现重复修饰符错误?

时间:2012-04-23 18:55:58

标签: java access-modifiers

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);
           }
        }
      }

我从this site复制了此代码,并尝试根据我们的环境进行调整。不幸的是,我无法得到$%!@#*($ thing to compile。我在主方法上遇到错误:错误:重复修饰符。

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

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

这是我第一次使用varargs ...我不确定是否〜这给了我一个问题?我扫描了Java Documentation,但没有发现任何危险信号。此外,当我将String...更改为String[]时,它编译得很好。我觉得我可能会得到一些空值,所以我宁愿在方法中留下String...

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

2 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:1)

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

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

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 );
        }
    }
}