How can I read a file to an InputStream then write it into an OutputStream in Scala?
You could do this:
Iterator
.continually (input.read)
.takeWhile (-1 !=)
.foreach (output.write)
If this is slow:
Iterator
.continually (input.read)
.takeWhile (-1 !=)
.foreach (output.write)
you can expand it:
val bytes = new Array[Byte](1024) //1024 bytes - Buffer size
Iterator
.continually (input.read(bytes))
.takeWhile (-1 !=)
.foreach (read=>output.write(bytes,0,read))
output.close()
Assignment statements always return Unit in Scala, so read = input.read
returns Unit, which never equals -1. You can do it like this:
while ({read = input.read; read != -1}) {
output.write(read)
}