操作缓存文件,熟悉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

熟悉Linux基本操作

计算机硬件基本知识CPU内部已经含有一些小的指令集,我们所使用的软件都要经过CPU内部的微指令集来达成。因此CPU可分为RISC(精简指令集)和CISC(复杂指令集)两种,前者有sun公司的SPARC...
  • qq_34620589
  • qq_34620589
  • 2017年04月20日 16:26
  • 861

Hbase的常用shell操作

1.进入hbase的shell界面: [root@lijie bin]# ./hbase shell 2.创建表 hbase(main):002:0> create 'tablename','cf1'...
  • qq_20641565
  • qq_20641565
  • 2017年02月08日 10:30
  • 750

电脑操作,熟悉?(1)

电脑操作,熟悉?(1)     以前以为自己对电脑的操作应该是达到熟悉(应用层)的水平了,但是通过这几个星期的资料查阅才深深地体会到原来自己是这么的菜。不说别的,单单多个关键词搜索这个技术以前就一直没...
  • minnan_ren
  • minnan_ren
  • 2009年07月11日 12:00
  • 292

sql server create file遇到操作系统错误5拒绝访问

create file遇到操作系统错误5拒绝访问 当在sql server 2008创建一个数据库时出现错误: 尝试打开或创建物理文件 'F:\stuDB_data.mdf' 时,CREATE F...
  • webjie
  • webjie
  • 2011年12月28日 19:27
  • 5927

【phpcms-v9】phpcms-v9中后台配置信息所对应的缓存文件

由于前台数据的展示大部分依赖于缓存文件,所以又必须将一些常用的缓存文件列出 1.url规则对应的缓存文件 2.基本设置对应的缓存文件 3.栏目配置信息对应的缓存文件 ...
  • yanhui_wei
  • yanhui_wei
  • 2012年11月20日 15:59
  • 1758

如何掌握pdf文件基本编辑操作

对于很多人来说PDF这种格式文档并不是最常用的一种文本格式,都习惯用word来编辑文本,因此对应pdf文件的使用不是太熟悉,比如说该如何去编辑PDF文档。其实对于PDF文件的编辑非常简单。下一个pdf...
  • m3257476260
  • m3257476260
  • 2016年03月09日 11:17
  • 452

tensorflow常见操作

1、tensorflow的基本运作 为了快速的熟悉TensorFlow编程,下面从一段简单的代码开始: import tensorflow as tf #定义‘符号’变量,也称为占位符 a =...
  • zuanfengxiao
  • zuanfengxiao
  • 2017年12月01日 19:57
  • 79

MongoDB的安装与增删改查语句

课程设计老师要求项目需要用到NoSQL数据库,所以看了一点MongoDB的资料,安装的数据库版本为当前2017/03/15之前发布的最新版本3.4.2,安装文件在官网下载的mongodb-win32-...
  • u010752082
  • u010752082
  • 2017年03月15日 20:30
  • 245

create file遇到操作系统错误5拒绝访问

create file遇到操作系统错误5拒绝访问 当在sql server 2014创建一个数据库时出现错误: 尝试打开或创建物理文件 'G:\Test.mdf' 时,CREATE...
  • suwu150
  • suwu150
  • 2016年06月04日 13:25
  • 5364

Discuz!开发之写入文件缓存

Discuz!拥有一套良好的缓存机制。不管是内存缓存、数据库缓存还是文件缓存,都做的非常优秀!当开发的功能需要大量的或者频繁的在数据库里读取时,而且数据库查询语句很复杂时,如果用户刷新一次页面就读取一...
  • lih062624
  • lih062624
  • 2017年04月03日 18:28
  • 3942
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:操作缓存文件,熟悉File操作
举报原因:
原因补充:

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