|
.collect()
the Path
s out of the
stream before moving or deleting them, like this:
stream.collect(Collectors.toList())If you don't, you will encounter errors like this:
java.nio.file.FileAlreadyExistsException: /home/username/path/to/fileor this:
java.io.UncheckedIOException: java.nio.file.NoSuchFileException: /home/username/path/to/file
Files.find()
). While a convenient method, the
Path
s in the resulting stream can't be deleted while
iterating over the stream. To do so results in the exceptions above.
First, collect the Stream
into a List
:
List<Path> listPaths = stream.collect(Collectors.toList())Then use a foreach loop to process the
Path
s:
for (Path path : listPaths) {
Copyright © 2024 Andrew Oliver