Do I need to File.close() after File.stream!()?

,

Do I need to close the file after the end of the stream is reached, if it was a file stream?
Also, does the file need to be closed if there was an error reading from the stream?
Or does Elixir close the file automatically?

Example:
try do
file_path
|> File.stream!()
|> Jason.decode()
|> IO.inspect()
after
File.close(???) # don’t really have a file handle after File.stream!()
end

As far as I know the File.close/1 should be used only with File.open/1. Only File.open/1 docs gives an example with File.close/1 call. See File.open/1 docs.

1 Like

The stream! will automatically close the file in the after_fun, see elixir/stream.ex at a64d42f5d3cb6c32752af9d3312897e8cd5bb7ec · elixir-lang/elixir · GitHub.

3 Likes