Skip to content

Instantly share code, notes, and snippets.

@tomer-ben-david
Created May 27, 2014 10:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomer-ben-david/1f2611db1d0851a65d43 to your computer and use it in GitHub Desktop.
Save tomer-ben-david/1f2611db1d0851a65d43 to your computer and use it in GitHub Desktop.
write bytes to file in scala
val byteArray: Array[Byte] = Array(1,2)
val bos = new BufferedOutputStream(new FileOutputStream(filename))
Stream.continually(bos.write(byteArray))
bos.close() // You may end up with 0 bytes file if not calling close.
@mulya
Copy link

mulya commented Mar 15, 2017

There is misusing of "continually". You don't have any operation to execute continually here. Makes absolutely the same as:

val byteArray: Array[Byte] = Array(1,2)
val bos = new BufferedOutputStream(new FileOutputStream(filename))
bos.write(byteArray)
bos.close() // You may end up with 0 bytes file if not calling close.

Here is good example of use "continually":

val bytes = new Array[Byte](1024) //1024 bytes - Buffer size
Stream
.continually (input.read(bytes))
.takeWhile (-1 !=)
.foreach (read=>output.write(bytes,0,read))
output.close()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment