Almost exactly four years ago, Cay Horstmann was complaining that
it wasn't possible to get a Stream out of a Scanner. Even confirmed Scannerophobes like Winston
thought that should be possible. In the Horstmann blog, somebody called Rémi said maybe Java9 would introduce a way to get a Stream from a Scanner. So I had a look about 4.00 o'clock today, and lo and behold, there are two/three methods: findAll() (overloaded) and tokens(). So I thought I would try them out:
java ScannerStreamDemo
Enter tokens: 0 to finish: Coderanch 1 2 69 is brilliant 4 ldhdl 123.45 0
244
Enter more tokens: 0 to finish: Coderanch 1 2 69 is brilliant 4 ldhdl 123.45 0
76
1+2+69+4=76; in the first case it was perceiving the 123 and the 45 as matching the regex supplied: 1+2+69+4+123+45=244.
If you don't have some means of stopping the Stream, it may turn into an infinite Stream.
There isn't a lines() method, but it should be easy enough to get lines from tokens() with something like
useDelimiter("\r|\n|\r\n") or
useDelimiter(System.lineSeparator())