0

I am working with Grails. I need to copy a file from one folder to another. Here are my attempts below ::

def wrapAll(){
    def uploadList = Upload.findAllByIsWrapped(false)
    if (uploadList){
        uploadList.each {
            def dist = new Dist(it.properties)
            dist.filePath = it.filePath.replace("upload","dist")
            def file = new File(it.filePath)

        }
    }
}

here it.filePath = web-app/apps/upload/test_txt_file.txt and dist.filePath = web-app/apps/dist/test_txt_file.txt

I want that first file will be copied in second location.

Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
Sumon Bappi
  • 1,937
  • 8
  • 38
  • 82
  • have you tried this ?http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java or this ? http://www.journaldev.com/861/4-ways-to-copy-file-in-java – V H May 05 '15 at 12:27
  • @vahid nice link , thanks a lot – Sumon Bappi May 06 '15 at 03:55

1 Answers1

0

The snippet below copies files the groovy way. Try this after injecting grailsApplication.

def source = grailsApplication.mainContext.getResource("dirLoc1/inputFile.json").file
def destination = grailsApplication.mainContext.getResource("dirLoc2/outputFile.json").file
destination.createNewFile()

destination.withDataOutputStream { os ->
    source.withDataInputStream { is -> 
        os << is 
    }
}
mohsenmadi
  • 2,277
  • 1
  • 23
  • 34