How to download and save a file from the internet using Scala?

前端 未结 4 698
礼貌的吻别
礼貌的吻别 2021-02-02 13:21

Basically I have a url/link to a text file online and I am trying to download it locally. For some reason, the text file that gets created/downloaded is blank. Open to any sugge

4条回答
  •  广开言路
    2021-02-02 13:42

    I know this is an old question, but I just came across a really nice way of doing this :

    import sys.process._
    import java.net.URL
    import java.io.File
    
    def fileDownloader(url: String, filename: String) = {
        new URL(url) #> new File(filename) !!
    }
    

    Hope this helps. Source.

    You can now simply use fileDownloader function to download the files.

    fileDownloader("http://ir.dcs.gla.ac.uk/resources/linguistic_utils/stop_words", "stop-words-en.txt")
    

提交回复
热议问题