【发布时间】:2012-04-23 18:55:58
【问题描述】:
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 复制了这段代码,并试图将其调整为适合我们的环境。不幸的是,我无法编译 $%!@#*($ 东西。我在 main 方法中不断收到错误:错误:重复修饰符。
public static void main(String... aArgs) throws FileNotFoundException 是错误标记的行。
我在这里看不到任何重复的修饰符,我所有的花括号和括号似乎都在正确的位置,我完全被难住了。
这是我第一次使用可变参数...我不确定~这是否给我带来了问题?我扫描了 Java 文档,但没有发现任何危险信号。此外,当我将String... 更改为String[] 时,它编译得很好。我觉得我可能会得到一些空值,所以我宁愿在方法中留下String...。
有人看到我遗漏的任何东西吗?我觉得我在看那个巴黎 春季脑筋急转弯....
【问题讨论】:
-
你能指出哪一行给你编译问题吗?
-
main(String... aArgsjava支持椭圆吗? -
您的代码的格式化方式既不常见,也不会使错误容易被发现。请考虑使用通用代码格式化程序(大多数 IDE 都集成了一个)。
-
第 8 行需要更改为 FileListing2.getFileListing(startingDirectory) 然后您需要添加/修复返回到 getFileListingNoSort。然后它应该编译。编辑:重复奥斯卡在下面所说的话。
标签: java access-modifiers