Safe Haskell | None |
---|---|
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.
- 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 ())
- fileEq :: FilePath -> FilePath -> IO Bool
Documentation
module System.IO
captureOutput :: IO a -> IO (String, a) Source
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 Source
A strict version of readFile
. When the string is produced, the entire
file will have been read into memory and the file handle will have been closed.
Closing the file handle does not rely on the garbage collector.
\(filter isHexDigit -> s) -> fmap (== s) $ withTempFile $ \file -> do writeFile file s; readFile' file
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.
\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 == return True (doesFileExist =<< withTempFile return) == return False withTempFile readFile' == return ""
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 == return True (doesDirectoryExist =<< withTempDir return) == return False withTempDir listFiles == return []
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.
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)