如何在Grails中将文件从一个文件夹复制到另一个文件夹[复制]

时间:2022-01-26 06:50:42

This question already has an answer here:

这个问题在这里已有答案:

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

我正在和Grails合作。我需要将文件从一个文件夹复制到另一个文件夹。以下是我的尝试::

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

这里it.filePath = web-app / apps / upload / test_txt_file.txt和dist.filePath = web-app / apps / dist / test_txt_file.txt

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

我希望第一个文件将被复制到第二个位置。

1 个解决方案

#1


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

下面的片段以文字的方式复制文件。注入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 
    }
}

#1


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

下面的片段以文字的方式复制文件。注入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 
    }
}