You are going about this all wrong, no Scanner
is required:
try (final InputStream is = A.class.getResourceAsStream("data.txt");
final Reader r = new InputStreamReader(is, StandardCharsets.UTF_8);
final BufferedReader br = new BufferedReader(r);
final Stream<String> lines = br.lines()) {
}
If you really want to use a Scanner
then it implements Iterator
so you can just do:
public Stream<String> streamScanner(final Scanner scanner) {
final Spliterator<String> splt = Spliterators.spliterator(scanner, Long.MAX_VALUE, Spliterator.ORDERED | Spliterator.NONNULL);
return StreamSupport.stream(splt, false)
.onClose(scanner::close);
}
P.S. you also don't seem to be closing resources. always close an InputStream
.