How to create a file on the current system due to grails controller action and save data from the database?

In my grails application, I urgently need to create a file on the current system, in which I need to save the information obtained from the table in the database. How to do this from inside the action.i controller has no idea about this. the body will help me.

Update:

I created the file as,

File File = New File ("file name.txt")

file.createNewFile ();

then I wrote the values ​​of the fields of the mysql db table in it as,

file<<patient.id file<<patient.name . . . 

it stores the data as continuous text, but I want to have a .doc file in which the data should be stored in a table. In googling, I found an APACHE POI to create a doc file, but I don’t understand how it works and how I should use it.

Thanks, Laxmi.p

+4
source share
2 answers

Not sure what exactly you want to save to a file, but below is an example of how easy it is to write String to a file using Apache-commons-io . must be included in grails

 import org.apache.commons.io.FileUtils; class SomeController{ def writeToFile = { def data = getSomeStringData(); def fileStore = new File("./path/to/files/ControllerOutput_${new Date()}.txt"); fileStore.createNewFile(); FileUtils.writeStringToFile(fileStore, data); println("your file was created @ {fileStore.absolutePath} and is ${fileStore.length()} bytes"); } } 

Does it help? If not, you need to explain exactly what you are looking for.

+5
source

This is a comment to Michael (unfortunately, I don't have a reputation yet to respond to the answers).

If you are struggling with the problem of how to specify the relative path from the context of your controller, this may help you: Therefore, if you have the following folder that you want to read / write files from / to "..

 /myproject/web-app/temp/ 

you can access the file as follows:

 import org.codehaus.groovy.grails.commons.ApplicationHolder as AH // getResource references to the web-app folder as root folder Resource resource = AH.getApplication().getParentContext().getResource("/temp/myfile.txt) 
0
source

All Articles