使用纯 Java NIO:
public static void main(final String[] args)
{
// This will give u all Root Directories. Like: C:, D:, ...
final Iterable<Path> rootDirs = FileSystems.getDefault().getRootDirectories();
for (final Path rootDir : rootDirs)
{
if (rootDir.startsWith("C:") == false)
continue;
// This will loop through every of this root directories
try (DirectoryStream<Path> stream = Files.newDirectoryStream(rootDir))
{
for (final Path file : stream)
{
System.out.println(file.getFileName());
}
}
catch (IOException | DirectoryIteratorException x)
{
System.err.println(x);
}
}
}