4
Can do with listFiles
or Files walk. java 8.
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
class Teste {
public static void main(String[] args) {
final File pasta = new File("C:\\teste");
listaArquivos(pasta);
listaArquivosJava8(pasta);
}
public static void listaArquivos(final File pasta) {
for (final File fileEntry : pasta.listFiles()) {
if (! fileEntry.isDirectory()) {
System.out.println(fileEntry.getName());
}
}
}
public static void listaArquivosJava8(final File pasta) {
try(Stream<Path> paths = Files.walk(Paths.get(pasta.toURI()))) {
paths.forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
System.out.println(filePath);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related (if not duplicated): http://answall.com/q/159121/3084
– cantoni