Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
More IO functions. The functions include ones for reading files with specific encodings, strictly reading files, and writing files with encodings. There are also some simple temporary file functions, more advanced alternatives can be found in the exceptions package.
Synopsis
- module System.IO
- captureOutput :: IO a -> IO (String, a)
- withBuffering :: Handle -> BufferMode -> IO a -> IO a
- readFileEncoding :: TextEncoding -> FilePath -> IO String
- readFileUTF8 :: FilePath -> IO String
- readFileBinary :: FilePath -> IO String
- readFile' :: FilePath -> IO String
- readFileEncoding' :: TextEncoding -> FilePath -> IO String
- readFileUTF8' :: FilePath -> IO String
- readFileBinary' :: FilePath -> IO String
- writeFileEncoding :: TextEncoding -> FilePath -> String -> IO ()
- writeFileUTF8 :: FilePath -> String -> IO ()
- writeFileBinary :: FilePath -> String -> IO ()
- withTempFile :: (FilePath -> IO a) -> IO a
- withTempDir :: (FilePath -> IO a) -> IO a
- newTempFile :: IO (FilePath, IO ())
- newTempDir :: IO (FilePath, IO ())
- newTempFileWithin :: FilePath -> IO (FilePath, IO ())
- newTempDirWithin :: FilePath -> IO (FilePath, IO ())
- fileEq :: FilePath -> FilePath -> IO Bool
Documentation
module System.IO
withBuffering :: Handle -> BufferMode -> IO a -> IO a Source #
Execute an action with a custom BufferMode
, a wrapper around
hSetBuffering
.
Read encoding
readFileEncoding :: TextEncoding -> FilePath -> IO String Source #
Like readFile
, but setting an encoding.
Strict reading
readFile' :: FilePath -> IO String #
The readFile'
function reads a file and
returns the contents of the file as a string.
The file is fully read before being returned, as with getContents'
.
Since: base-4.15.0.0
readFileEncoding' :: TextEncoding -> FilePath -> IO String Source #
A strict version of readFileEncoding
, see readFile'
for details.
readFileUTF8' :: FilePath -> IO String Source #
A strict version of readFileUTF8
, see readFile'
for details.
readFileBinary' :: FilePath -> IO String Source #
A strict version of readFileBinary
, see readFile'
for details.
Write with encoding
writeFileEncoding :: TextEncoding -> FilePath -> String -> IO () Source #
Write a file with a particular encoding.
writeFileUTF8 :: FilePath -> String -> IO () Source #
Write a file with the utf8
encoding.
\s -> withTempFile $ \file -> do writeFileUTF8 file s; fmap (== s) $ readFileUTF8' file
writeFileBinary :: FilePath -> String -> IO () Source #
Write a binary file.
\(ASCIIString s) -> withTempFile $ \file -> do writeFileBinary file s; fmap (== s) $ readFileBinary' file
Temporary files
withTempFile :: (FilePath -> IO a) -> IO a Source #
Create a temporary file in the temporary directory. The file will be deleted
after the action completes (provided the file is not still open).
The FilePath
will not have any file extension, will exist, and will be zero bytes long.
If you require a file with a specific name, use withTempDir
.
withTempFile doesFileExist == pure True (doesFileExist =<< withTempFile pure) == pure False withTempFile readFile' == pure ""
withTempDir :: (FilePath -> IO a) -> IO a Source #
Create a temporary directory inside the system temporary directory. The directory will be deleted after the action completes.
withTempDir doesDirectoryExist == pure True (doesDirectoryExist =<< withTempDir pure) == pure False withTempDir listFiles == pure []
newTempFile :: IO (FilePath, IO ()) Source #
Provide a function to create a temporary file, and a way to delete a
temporary file. Most users should use withTempFile
which
combines these operations.
newTempDir :: IO (FilePath, IO ()) Source #
Provide a function to create a temporary directory, and a way to delete a
temporary directory. Most users should use withTempDir
which
combines these operations.
newTempFileWithin :: FilePath -> IO (FilePath, IO ()) Source #
Like newTempFile
but using a custom temporary directory.
newTempDirWithin :: FilePath -> IO (FilePath, IO ()) Source #
Like newTempDir
but using a custom temporary directory.
File comparison
fileEq :: FilePath -> FilePath -> IO Bool Source #
Returns True
if both files have the same content.
Raises an error if either file is missing.
fileEq "does_not_exist1" "does_not_exist2" == undefined fileEq "does_not_exist" "does_not_exist" == undefined withTempFile $ \f1 -> fileEq "does_not_exist" f1 == undefined withTempFile $ \f1 -> withTempFile $ \f2 -> fileEq f1 f2 withTempFile $ \f1 -> withTempFile $ \f2 -> writeFile f1 "a" >> writeFile f2 "a" >> fileEq f1 f2 withTempFile $ \f1 -> withTempFile $ \f2 -> writeFile f1 "a" >> writeFile f2 "b" >> notM (fileEq f1 f2)