Skip to content

Instantly share code, notes, and snippets.

@taisyo7333
Created July 17, 2015 13:50
Show Gist options
  • Save taisyo7333/337554f967f29d844cd9 to your computer and use it in GitHub Desktop.
Save taisyo7333/337554f967f29d844cd9 to your computer and use it in GitHub Desktop.
Haskell ByteString Samle
import System.Environment
import System.Directory -- openTempFile , removeFile
import System.IO
import Control.Exception -- bracketOnError
import qualified Data.ByteString.Lazy as B
{-
This code is referenced from "Learn You a Haskell for Great Good!!"
-}
main = do
( filename1:filename2:_) <- getArgs
copy filename1 filename2
copy :: String -> String -> IO ()
copy source dest = do
contents <- B.readFile source
bracketOnError (openTempFile "." "temp")
(\(tempPath, tempHandle) -> do
hClose tempHandle
removeFile tempPath)
(\(tempPath,tempHandle) -> do
B.hPutStr tempHandle contents
hClose tempHandle
renameFile tempPath dest
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment