readFile -package:ghc
The
readFile function reads a file and returns the contents of
the file as a string.
The file is read lazily, on demand, as with
getContents.
This operation may fail with the same errors as
hGetContents
and
openFile.
Examples
>>> readFile "~/hello_world"
"Greetings!"
>>> take 5 <$> readFile "/dev/zero"
"\NUL\NUL\NUL\NUL\NUL"
Read an entire file
lazily into a
ByteString.
The
Handle will be held open until EOF is encountered.
Note that this function's implementation relies on
hGetContents. The reader is advised to read its documentation.
The
readFile function reads a file and returns the contents of
the file as a string. The entire file is read strictly, as with
getContents.
Beware that this function (similarly to
readFile) is
locale-dependent. Unexpected system locale may cause your application
to read corrupted data or throw runtime exceptions about "invalid
argument (invalid byte sequence)" or "invalid argument (invalid
character)". This is also slow, because GHC first converts an entire
input to UTF-32, which is afterwards converted to UTF-8.
If your data is UTF-8, using
decodeUtf8 .
readFile is a much faster and safer alternative.
The
readFile function reads a file and returns the contents of
the file as a string. The entire file is read strictly, as with
getContents.
Read a file and return its contents as a string. The file is read
lazily, as with
getContents.
Beware that this function (similarly to
readFile) is
locale-dependent. Unexpected system locale may cause your application
to read corrupted data or throw runtime exceptions about "invalid
argument (invalid byte sequence)" or "invalid argument (invalid
character)". This is also slow, because GHC first converts an entire
input to UTF-32, which is afterwards converted to UTF-8.
If your data is UTF-8, using
decodeUtf8 .
readFile is a much faster and safer alternative.
The
readFile function reads a file and returns the contents of
the file as a string. The file is read lazily, on demand, as with
getContents.
Read file.
>>> foreach fail BS.putStr (readFile "servant.cabal")
cabal-version: 3.0
name: servant
...
The
readFile function reads a file and returns the contents of
the file as a string. The file is read strictly, as with
getContents.
Warning: readFile depends on the system's locale settings
and can throw unexpected exceptions.Use readFileBS or
readFileLBS instead.
Read the lines of a file, using a function of the type:
'
Stream (Of String) IO () ->
IO a' to turn the stream into a value of type '
IO
a'.
>>> S.writeFile "lines.txt" $ S.take 2 S.stdinLn
hello<Enter>
world<Enter>
>>> S.readFile "lines.txt" S.print
"hello"
"world"
Read lines from a file, automatically opening and closing the file as
necessary
Read in the entire content of a binary file.
This computation throws
IOError on failure. See “Classifying
I/O errors” in the
System.IO.Error documentation for
information on why the failure occured.
Read an entire file strictly into a
Bytes.
Read an entire file strictly into chunks. If reading from a regular
file, this makes an effort read the file into a single chunk.
Read an entire file into a chunked
ByteStream IO ().
The handle will be held open until EOF is encountered. The block
governed by
runResourceT will end with the closing of any
handles opened.
>>> :! cat hello.txt
Hello world.
Goodbye world.
>>> runResourceT $ Q.stdout $ Q.readFile "hello.txt"
Hello world.
Goodbye world.