Erlang Central

Read File to List Alternate

From ErlangCentral Wiki


You want to read the lines from a file and store them in a list, efficiently.


I read somewhere a while back, possibly in Joe Armstrong's book that an alternative, efficient approach for converting the file contents to a list is to read the file into a binary, since that is a BIF routine, and then converting the binary to a list.



readfile(FileName) ->
    {ok, Binary} = file:read_file(FileName),
     Lines = string:tokens(erlang:binary_to_list(Binary), "\n").