Write Stream<String> to a file?

Zhong Yu zhong.j.yu at gmail.com
Tue Nov 19 21:24:10 PST 2013


Thanks, it works.

<pedantry>If a method accepts an Iterable, you never now how many
times it may invoke iterator(). So in theory passing a Stream to the
method isn't safe. </>

I was wishing for something like a Collector.

Zhong Yu


On Tue, Nov 19, 2013 at 9:27 PM, Sam Pullara <spullara at gmail.com> wrote:
> My solution seems the right way to go and is basically a mirror of that method. It should probably have an override that takes a stream rather than just an Iterable to make it more obvious. Here is the form with all the static imports:
>
>     Stream<String> s = Files.lines(get(from), UTF_8);
>     Files.write(get(to), s::iterator, UTF_8);
>
>
> Sam
>
> On Nov 19, 2013, at 6:24 PM, Zhong Yu <zhong.j.yu at gmail.com> wrote:
>
>> A problem with this is that most write() methods in java.io throw
>> checked IOException. PrintWriter does not throw checked exceptions -
>> it swallows them which is not good either.
>>
>> On Tue, Nov 19, 2013 at 6:47 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
>>> How about:
>>>
>>>    stream.forEachOrdered(writer::write);
>>>
>>>
>>> On 11/19/2013 6:19 PM, Zhong Yu wrote:
>>>>
>>>> We have convenience methods to read lines from a file:
>>>>     Stream<String> Files.lines()
>>>>
>>>> What's Java's idiomatic way to write a Stream<String> to a file?
>>>>
>>>> Zhong Yu
>>>>
>>>
>>
>


More information about the lambda-dev mailing list