2008年8月4日

Tricks of Groovy - Copy File

With Groovy you can do file copying in few lines scripts list below.
  1. //importjava.net commons
    FileTools.copyFile("foo.txt", "bar.txt");
  2. //antbuilder (no import needed!)
    ( new AntBuilder ( ) ).copy ( file : 'blah' , tofile : 'fobar' )
  3. //apache commons io
    org.apache.commons.io.FileUtils.copyFile(srcfile, targetFile)
  4. //simply copy text file with write/text methods
    new File('SRC_FILE').write(new File('TO_FILE').text)
  5. static void copyFile(File source, File destination) {
    def reader = source.newReader()
    destination.withWriter { writer ->
    writer << reader
    }
    reader.close()
    }
  6. //with shell under unix
    'cp SRC TARGET'.execute()
  7. //with shell under window
    'copy SRC TARGET'.execute()

沒有留言:

張貼留言

lyhcode by lyhcode
歡迎轉載,請務必註明出處!