Given the following directory structure:
test -|
| - Finder.class
| - testdir -|
| - subdir
| - subdir2
| - testfile.txt
If test, testdir
, subdir
, and subdir2
are all directories, and Finder.class and testfile.txt
are files, and given:
import java.io.*; public class Main { public static void main(String[] args) throws IOException { String[] files = new String[100]; File dir = new File(args[0]); files = dir.list(); System.out.println(files.length); } }
And, if the code compiles, the invocation:.
java Main testdir
What is the result?
C is correct.
File I/O stuff is risky, but it's okay for main()
to throw an exception.
The list()
method returns a String[] that contains the names of both the files AND subdirectories within the directory specified.
Note that the current directory and its contents are not included.