Reputation: 7076
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);
}
}
}
I copied this code from this site and am trying to adapt it for our environment. Unfortunately, I can't get the $%!@#*($ thing to compile. I keep getting an error at the main method: error: repeated modifier.
public static void main(String... aArgs) throws FileNotFoundException
is the line flagged for the error.
I don't see any duplicate modifiers in here, All my curly braces and parens appear to be in the right places, and I am completely stumped.
This is my first time working with varargs...I'm not sure if ~that's~ giving me an issue? I scanned Java Documentation, but didn't spot any red flags. Besides, it compiles just fine when I change String...
to String[]
. I feel I may get some null values, so I'd prefer to leave String...
in the method.
Anyone see anything that I'm missing? I feel like I'm looking at that Paris in the the spring brainteaser....
Upvotes: 3
Views: 21480
Reputation: 3417
I could not reproduce your repeated modifier error (using JDK 1.7.0), but the return statement of the method getFileListingNoSort
is not in the method body.
Please format your code so that it makes sense and you will see such issues. The following compiles fine:
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 );
}
}
}
Upvotes: 1
Reputation: 236112
In the method getFileListingNoSort()
the line return result;
is outside of the method, move it one line up to put it inside, where it belongs.
Upvotes: 7