File download problem in my scala code

前端 未结 2 816
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-15 16:37

I have written the following scala code to download a file . The file gets downloaded correctly but also an exception is thrown. The code is as follows :

var         


        
相关标签:
2条回答
  • 2021-01-15 16:49

    An alternative option is to use the system commands which is much cleaner and faster from what I can tell.

    import sys.process._
    import java.net.URL
    import java.io.File
    
    new URL("http://somehost.com/file.doc") #> new File("test.doc") !!
    
    0 讨论(0)
  • 2021-01-15 16:51

    Scala returns type Unit, not the type of the value being assigned, with an assignment statement. So

    numRead = in.read(buffer)
    

    never returns -1; it doesn't even return an integer. You can write

    while( { numRead = in.read(buffer); numRead != -1 } ) out.write(buffer, 0, numRead)
    

    or you can go for a more functional style with

    Iterator.continually(in.read(buffer)).takeWhile(_ != -1).foreach(n => out.write(buffer,0,n))
    

    Personally, I prefer the former since it's shorter (and relies less on iterator evaluation happening the way "it ought to").

    0 讨论(0)
提交回复
热议问题