text-2.0.2: An efficient packed Unicode text type.

Copyright | (c) 2009 2010 Bryan O'Sullivan (c) 2009 Simon Marlow
License | BSD-style
Maintainer | bos@serpentine.com
Portability | GHC
Safe Haskell | Trustworthy
Language | Haskell2010

Data.Text.IO

Contents

- File-at-a-time operations
- Operations on handles
- Special cases for standard input and output

Description

Efficient locale-sensitive support for text I/O.

The functions in this module obey the runtime system's locale, character set encoding, and line ending conversion settings.

If you know in advance that you will be working with data that has a specific encoding (e.g. UTF-8), and your application is highly performance sensitive, you may find that it is faster to perform I/O with bytestrings and to encode and decode yourself than to use the functions in this module.

Synopsis

- readFile :: FilePath -> IO Text
- writeFile :: FilePath -> Text -> IO ()
- appendFile :: FilePath -> Text -> IO ()
- hGetContents :: Handle -> IO Text
- hGetChunk :: Handle -> IO Text
- hGetLine :: Handle -> IO Text
- hPutStr :: Handle -> Text -> IO ()
- hPutStrLn :: Handle -> Text -> IO ()
- interact :: (Text -> Text) -> IO ()
- getContents :: IO Text
- getLine :: IO Text
- putStr :: Text -> IO ()
- putStrLn :: Text -> IO ()

# File-at-a-time operations

readFile :: FilePath -> IO Text Source #

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.

writeFile :: FilePath -> Text -> IO () Source #

Write a string to a file. The file is truncated to zero length before writing begins.

appendFile :: FilePath -> Text -> IO () Source #

Write a string to the end of a file.

# Operations on handles

hGetContents :: Handle -> IO Text Source #

Read the remaining contents of a `Handle` as a string. The `Handle` is closed once the contents have been read, or if an exception is thrown.

Internally, this function reads a chunk at a time from the lower-level buffering abstraction, and concatenates the chunks into a single string once the entire file has been read.

As a result, it requires approximately twice as much memory as its result to construct its result. For files more than a half of available RAM in size, this may result in memory exhaustion.

hGetChunk :: Handle -> IO Text Source #

Experimental. Read a single chunk of strict text from a `Handle`. The size of the chunk depends on the amount of input currently buffered.

This function blocks only if there is no data available, and EOF has not yet been reached. Once EOF is reached, this function returns an empty string instead of throwing an exception.

hGetLine :: Handle -> IO Text Source #

Read a single line from a handle.

hPutStr :: Handle -> Text -> IO () Source #

Write a string to a handle.

hPutStrLn :: Handle -> Text -> IO () Source #

Write a string to a handle, followed by a newline.

# Special cases for standard input and output

interact :: (Text -> Text) -> IO () Source #

The `interact` function takes a function of type `Text -> Text` as its argument. The entire input from the standard input device is passed to this function as its argument, and the resulting string is output on the standard output device.

getContents :: IO Text Source #

Read all user input on `stdin` as a single string.

getLine :: IO Text Source #

Read a single line of user input from `stdin`.

putStr :: Text -> IO () Source #

Write a string to `stdout`.

putStrLn :: Text -> IO () Source #

Write a string to `stdout`, followed by a newline.