Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35828 > unrolled thread

How do you close the stream with Files.write?

Started byKnute Johnson <nospam@rabbitbrush.frazmtn.com>
First post2016-10-19 15:55 -0500
Last post2016-10-19 16:12 -0500
Articles 3 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  How do you close the stream with Files.write? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-10-19 15:55 -0500
    Re: How do you close the stream with Files.write? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-10-19 17:07 -0400
      Re: How do you close the stream with Files.write? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-10-19 16:12 -0500

#35828 — How do you close the stream with Files.write?

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2016-10-19 15:55 -0500
SubjectHow do you close the stream with Files.write?
Message-ID<nu8mjm$jhh$1@dont-email.me>
java.nio.file.Files(Path path, Iterable<? extends CharSequence> lines, 
OpenOption... options) appears to have no way to close the underlying 
stream?

Thanks,

-- 

Knute Johnson

[toc] | [next] | [standalone]


#35829

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2016-10-19 17:07 -0400
Message-ID<nu8n9u$mbu$1@dont-email.me>
In reply to#35828
On 10/19/2016 4:55 PM, Knute Johnson wrote:
> java.nio.file.Files(Path path, Iterable<? extends CharSequence> lines,
> OpenOption... options) appears to have no way to close the underlying
> stream?

     (I guess there's a `write' missing before the parameters.)

     Looks like the file auto-closes at the end of the operation.  From
the JavaDoc for the three-parameter write():

	This method works as if invoking it were equivalent to
	evaluating the expression:

	Files.write(path, lines, StandardCharsets.UTF_8, options);

... and the JavaDoc for the latter says

	[...] The method ensures that the file is closed when all lines
	have been written (or an I/O error or other runtime exception
	is thrown). [...]

     So it appears you're only in trouble if you want the stream
to remain open.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#35831

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2016-10-19 16:12 -0500
Message-ID<nu8njs$niq$1@dont-email.me>
In reply to#35829
On 10/19/2016 16:07, Eric Sosman wrote:
> On 10/19/2016 4:55 PM, Knute Johnson wrote:
>> java.nio.file.Files(Path path, Iterable<? extends CharSequence> lines,
>> OpenOption... options) appears to have no way to close the underlying
>> stream?
>
>     (I guess there's a `write' missing before the parameters.)
>
>     Looks like the file auto-closes at the end of the operation.  From
> the JavaDoc for the three-parameter write():
>
>     This method works as if invoking it were equivalent to
>     evaluating the expression:
>
>     Files.write(path, lines, StandardCharsets.UTF_8, options);
>
> ... and the JavaDoc for the latter says
>
>     [...] The method ensures that the file is closed when all lines
>     have been written (or an I/O error or other runtime exception
>     is thrown). [...]
>
>     So it appears you're only in trouble if you want the stream
> to remain open.
>

Thanks Eric, I missed that as I was looking at the docs for the other 
write method.  I did look in the source and it is clearly closed there.

-- 

Knute Johnson

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web