Skip to main content
Active reading [<https://www.youtube.com/watch?v=1Dax90QyXgI&t=17m54s> <https://www.youtube.com/watch?v=1Dax90QyXgI&t=19m05s>]. Added a lead.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

Use:

public void listFilesForFolder(final File folder) { for (final File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) { listFilesForFolder(fileEntry); } else { System.out.println(fileEntry.getName()); } } } final File folder = new File("/home/you/Desktop"); listFilesForFolder(folder); 

The Files.walk API is available from Java 8.

try (Stream<Path> paths = Files.walk(Paths.get("/home/you/Desktop"))) { paths .filter(Files::isRegularFile) .forEach(System.out::println); } 

The example uses the try-with-resources pattern recommended in the API guide. It ensures that no matter circumstances, the stream will be closed.

public void listFilesForFolder(final File folder) { for (final File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) { listFilesForFolder(fileEntry); } else { System.out.println(fileEntry.getName()); } } } final File folder = new File("/home/you/Desktop"); listFilesForFolder(folder); 

Files.walk API is available from Java 8.

try (Stream<Path> paths = Files.walk(Paths.get("/home/you/Desktop"))) { paths .filter(Files::isRegularFile) .forEach(System.out::println); } 

The example uses try-with-resources pattern recommended in API guide. It ensures that no matter circumstances the stream will be closed.

Use:

public void listFilesForFolder(final File folder) { for (final File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) { listFilesForFolder(fileEntry); } else { System.out.println(fileEntry.getName()); } } } final File folder = new File("/home/you/Desktop"); listFilesForFolder(folder); 

The Files.walk API is available from Java 8.

try (Stream<Path> paths = Files.walk(Paths.get("/home/you/Desktop"))) { paths .filter(Files::isRegularFile) .forEach(System.out::println); } 

The example uses the try-with-resources pattern recommended in the API guide. It ensures that no matter circumstances, the stream will be closed.

Cleaner use of streams API
Source Link
rich
  • 19.5k
  • 11
  • 80
  • 104
public void listFilesForFolder(final File folder) { for (final File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) { listFilesForFolder(fileEntry); } else { System.out.println(fileEntry.getName()); } } } final File folder = new File("/home/you/Desktop"); listFilesForFolder(folder); 

Files.walk API is available from Java 8.

try (Stream<Path> paths = Files.walk(Paths.get("/home/you/Desktop"))) { paths.forEach(filePath -> { if .filter(Files.::isRegularFile(filePath)) {   .forEach(System.out.::println(filePath); } }); } 

The example uses try-with-resources pattern recommended in API guide. It ensures that no matter circumstances the stream will be closed.

public void listFilesForFolder(final File folder) { for (final File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) { listFilesForFolder(fileEntry); } else { System.out.println(fileEntry.getName()); } } } final File folder = new File("/home/you/Desktop"); listFilesForFolder(folder); 

Files.walk API is available from Java 8.

try(Stream<Path> paths = Files.walk(Paths.get("/home/you/Desktop"))) { paths.forEach(filePath -> { if (Files.isRegularFile(filePath)) {   System.out.println(filePath); } }); } 

The example uses try-with-resources pattern recommended in API guide. It ensures that no matter circumstances the stream will be closed.

public void listFilesForFolder(final File folder) { for (final File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) { listFilesForFolder(fileEntry); } else { System.out.println(fileEntry.getName()); } } } final File folder = new File("/home/you/Desktop"); listFilesForFolder(folder); 

Files.walk API is available from Java 8.

try (Stream<Path> paths = Files.walk(Paths.get("/home/you/Desktop"))) { paths .filter(Files::isRegularFile) .forEach(System.out::println); } 

The example uses try-with-resources pattern recommended in API guide. It ensures that no matter circumstances the stream will be closed.

Make an Java8 API example follow API guide as many people just copy&paste the code.
Source Link
public void listFilesForFolder(final File folder) { for (final File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) { listFilesForFolder(fileEntry); } else { System.out.println(fileEntry.getName()); } } } final File folder = new File("/home/you/Desktop"); listFilesForFolder(folder); 

Edit: ThisFiles.walk API is now available from Java 8.

try(Stream<Path> paths = Files.walk(Paths.get("/home/you/Desktop"))) { paths.forEach(filePath -> {   if (Files.isRegularFile(filePath)) {   System.out.println(filePath);   }  }); }  

The example uses try-with-resources pattern recommended in API guide. It ensures that no matter circumstances the stream will be closed.

public void listFilesForFolder(final File folder) { for (final File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) { listFilesForFolder(fileEntry); } else { System.out.println(fileEntry.getName()); } } } final File folder = new File("/home/you/Desktop"); listFilesForFolder(folder); 

Edit: This API is now available from Java 8.

Files.walk(Paths.get("/home/you/Desktop")).forEach(filePath -> { if (Files.isRegularFile(filePath)) { System.out.println(filePath); } }); 
public void listFilesForFolder(final File folder) { for (final File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) { listFilesForFolder(fileEntry); } else { System.out.println(fileEntry.getName()); } } } final File folder = new File("/home/you/Desktop"); listFilesForFolder(folder); 

Files.walk API is available from Java 8.

try(Stream<Path> paths = Files.walk(Paths.get("/home/you/Desktop"))) { paths.forEach(filePath -> {   if (Files.isRegularFile(filePath)) {   System.out.println(filePath);   }  }); }  

The example uses try-with-resources pattern recommended in API guide. It ensures that no matter circumstances the stream will be closed.

Updated alternative answer for Java 8.
Source Link
rich
  • 19.5k
  • 11
  • 80
  • 104
Loading
Closing parenthesis went missing
Source Link
Veger
  • 38k
  • 11
  • 110
  • 118
Loading
Source Link
rich
  • 19.5k
  • 11
  • 80
  • 104
Loading