操作缓存文件,熟悉File操作

转载 2015年07月07日 21:55:27
-- file: ch07/tempfile.hs
import System.IO
import System.Directory(getTemporaryDirectory, removeFile)
import Control.Exception(finally)

main :: IO ()
main = withTempFile "mytemp.txt" myAction

{- The guts of the program.  Called with the path and handle of a temporary
file.  When this function exits, that file will be closed and deleted
because myAction was called from withTempFile. -}
myAction :: FilePath -> Handle -> IO ()
myAction tempname temph =
    do -- Start by displaying a greeting on the terminal
        putStrLn "Welcome to tempfile.hs"
        putStrLn $ "I have a temporary file at " ++ tempname

        -- Let's see what the initial position is
        pos <- hTell temph
        putStrLn $ "My initial position is " ++ show pos

        -- Now, write some data to the temporary file
        let tempdata = show [1..10]
        putStrLn $ "Writing one line containing " ++
            show (length tempdata) ++ " bytes: " ++
               tempdata
        hPutStrLn temph tempdata

        -- Get our new position.  This doesn't actually modify pos
        -- in memory, but makes the name "pos" correspond to a different
        -- value for the remainder of the "do" block.
        pos <- hTell temph
        putStrLn $ "After writing, my new position is " ++ show pos

        -- Seek to the beginning of the file and display it
        putStrLn $ "The file content is: "
        hSeek temph AbsoluteSeek 0

        -- hGetContents performs a lazy read of the entire file
        c <- hGetContents temph

        -- Copy the file byte-for-byte to stdout, followed by \n
        putStrLn c

        -- Let's also display it as a Haskell literal
        putStrLn $ "Which could be expressed as this Haskell literal:"
        print c

{- This function takes two parameters: a filename pattern and another
function.  It will create a temporary file, and pass the name and Handle
of that file to the given function.

The temporary file is created with openTempFile.  The directory is the one
indicated by getTemporaryDirectory, or, if the system has no notion of
a temporary directory, "." is used.  The given pattern is passed to
openTempFile.

After the given function terminates, even if it terminates due to an
exception, the Handle is closed and the file is deleted. -}
withTempFile :: String -> (FilePath -> Handle -> IO a) -> IO a
withTempFile pattern func =
    do -- The library ref says that getTemporaryDirectory may raise on
       -- exception on systems that have no notion of a temporary directory.
       -- So, we run getTemporaryDirectory under catch.  catch takes
       -- two functions: one to run, and a different one to run if the
       -- first raised an exception.  If getTemporaryDirectory raised an
       -- exception, just use "." (the current working directory).
       tempdir <- getTemporaryDirectory
       (tempfile, temph) <- openTempFile tempdir pattern

       -- Call (func tempfile temph) to perform the action on the temporary
       -- file.  finally takes two actions.  The first is the action to run.
       -- The second is an action to run after the first, regardless of
       -- whether the first action raised an exception.  This way, we ensure
       -- the temporary file is always deleted.  The return value from finally
       -- is the first action's return value.
       finally (func tempfile temph)
               (do hClose temph
                   removeFile tempfile)
上面的代码还是好理解的,不过看和写是两码事(以此告诫要多动手),函数式处理File文件稍微不一样的如上面openTempleFile返回Handle,Handle可以不立即处理,这是常见的IO处理,haskell还有更好的惰性IO

相关文章推荐

File文件的操作

  • 2015年11月04日 15:06
  • 1.14MB
  • 下载

UTL_FILE操作文件代码

  • 2010年07月16日 12:42
  • 4KB
  • 下载

scala-03:Tuple、Array、Map与file文件操作入门实战

一 Tuple 元祖 1,在sclal中有用的容器对象是元祖: Tuple,元组是n个对象的一个聚集(Map是n=2个对象的聚集),与列表一样,元素也是不可变的,但与列表不同,在一个元祖可以包含...

文件操作大全(file)

  • 2008年01月03日 17:33
  • 7KB
  • 下载

VC--文件操作函数(file)

  • 2008年11月23日 18:06
  • 47KB
  • 下载

C文件流I/O操作:FILE类型

一.简介     所谓“文件”,一般是指存在外部介质上的数据的集合,一批数据是以文件的形式存放在外部介质(如磁盘、光盘和U盘)上的,操作系统以文件为单位对数据进行管理。对用户来说,常用的文件有两...

c语言文件操作open_file

  • 2009年09月07日 17:11
  • 162B
  • 下载

示例1.1 File类操作文件

  • 2016年06月02日 20:16
  • 6KB
  • 下载

cordova-plugin-file 文件操作整理(二)

Cordove File实例Demo 一、cordova.file获取系统目录 //当前程序的目录 console.info(cordova.file.applicationDirectory); /...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:操作缓存文件,熟悉File操作
举报原因:
原因补充:

(最多只允许输入30个字)