Skip to main content
better information regarding java 8 and closing streams
Source Link
Julian Pieles
  • 4.1k
  • 2
  • 27
  • 33

In Java 8 you can do this

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .forEach(System.out::println); 

which will print all files in a folder while excluding all directories. If you need a list, the following will do:

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .collect(Collectors.toList()) 

If you want to return List<File> instead of List<Path> just map it:

List<File> filesInFolder = Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .map(Path::toFile) .collect(Collectors.toList()); 

You also need to make sure to close the stream! Otherwise you might run into an exception telling you that too many files are open. Read Read here for more.here for more information.

In Java 8 you can do this

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .forEach(System.out::println); 

which will print all files in a folder while excluding all directories. If you need a list, the following will do:

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .collect(Collectors.toList()) 

If you want to return List<File> instead of List<Path> just map it:

List<File> filesInFolder = Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .map(Path::toFile) .collect(Collectors.toList()); 

You also need to make sure to close the stream! Otherwise you might run into an exception telling you that too many files are open. Read here for more.

In Java 8 you can do this

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .forEach(System.out::println); 

which will print all files in a folder while excluding all directories. If you need a list, the following will do:

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .collect(Collectors.toList()) 

If you want to return List<File> instead of List<Path> just map it:

List<File> filesInFolder = Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .map(Path::toFile) .collect(Collectors.toList()); 

You also need to make sure to close the stream! Otherwise you might run into an exception telling you that too many files are open. Read here for more information.

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

In Java 8 you can do this

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .forEach(System.out::println); 

which will print all files in a folder while excluding all directories. If you need a list, the following will do:

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .collect(Collectors.toList()) 

If you want to return List<File> instead of List<Path> just map it:

List<File> filesInFolder = Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .map(Path::toFile) .collect(Collectors.toList()); 

You also need to make sure to close the stream! Otherwise you might run into an exception telling you that too many files are open. Read here for more.Read here for more.

In Java 8 you can do this

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .forEach(System.out::println); 

which will print all files in a folder while excluding all directories. If you need a list, the following will do:

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .collect(Collectors.toList()) 

If you want to return List<File> instead of List<Path> just map it:

List<File> filesInFolder = Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .map(Path::toFile) .collect(Collectors.toList()); 

You also need to make sure to close the stream! Otherwise you might run into an exception telling you that too many files are open. Read here for more.

In Java 8 you can do this

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .forEach(System.out::println); 

which will print all files in a folder while excluding all directories. If you need a list, the following will do:

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .collect(Collectors.toList()) 

If you want to return List<File> instead of List<Path> just map it:

List<File> filesInFolder = Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .map(Path::toFile) .collect(Collectors.toList()); 

You also need to make sure to close the stream! Otherwise you might run into an exception telling you that too many files are open. Read here for more.

added 214 characters in body
Source Link
Julian Pieles
  • 4.1k
  • 2
  • 27
  • 33

In Java 8 you can do this

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .forEach(System.out::println); 

which will print all files in a folder while excluding all directories. If you need a list, the following will do:

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .collect(Collectors.toList()) 

If you want to return List<File> instead of List<Path> just map it:

List<File> filesInFolder = Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .map(Path::toFile) .collect(Collectors.toList()); 

You also need to make sure to close the stream! Otherwise you might run into an exception telling you that too many files are open. Read here for more.

In Java 8 you can do this

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .forEach(System.out::println); 

which will print all files in a folder while excluding all directories. If you need a list, the following will do:

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .collect(Collectors.toList()) 

If you want to return List<File> instead of List<Path> just map it:

List<File> filesInFolder = Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .map(Path::toFile) .collect(Collectors.toList()); 

In Java 8 you can do this

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .forEach(System.out::println); 

which will print all files in a folder while excluding all directories. If you need a list, the following will do:

Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .collect(Collectors.toList()) 

If you want to return List<File> instead of List<Path> just map it:

List<File> filesInFolder = Files.walk(Paths.get("/path/to/folder")) .filter(Files::isRegularFile) .map(Path::toFile) .collect(Collectors.toList()); 

You also need to make sure to close the stream! Otherwise you might run into an exception telling you that too many files are open. Read here for more.

added 75 characters in body
Source Link
Julian Pieles
  • 4.1k
  • 2
  • 27
  • 33
Loading
added 1 character in body
Source Link
Julian Pieles
  • 4.1k
  • 2
  • 27
  • 33
Loading
added 341 characters in body
Source Link
Julian Pieles
  • 4.1k
  • 2
  • 27
  • 33
Loading
Source Link
Julian Pieles
  • 4.1k
  • 2
  • 27
  • 33
Loading