grails
  • download
  • 2013-08-21 47 views 0 likes 
    0

    想寫一個多文件,然後將其下載:Grails的寫在內存中的文件只寫一行

    def download() { 
        file.write("line1\n") 
        file.write("line2\n") 
        response.setHeader "Content-disposition", "attachment; filename=testalex.txt" 
        response.contentType = 'text-plain' 
        response.outputStream << file.text 
        response.outputStream.flush() 
    } 
    

    但該文件只顯示2號線。這是什麼原因?謝謝!

    回答

    2

    @ Sergio的方法或者使用append爲 「2號線」 :)

    ...... 
    file.write("line1\n") 
    file.append("line2\n") 
    ...... 
    

    append文本追加到文件的末尾。儘管我喜歡寫文章(@塞爾吉奧的方法)。

    2

    根據the docs

    寫(字符串文本)
    寫入文本文件。

    因此,您在每次使用write()時都要替換文件中的內容。您可以查看關於Input and Output的Groovy文檔。例如:

    file.withWriter { out -> 
        out.writeLine("line1") //no need to add the \n, the out will handle. 
        out.writeLine("line2") 
    } 
    
    相關問題