BufferedStream Scala (or Java) Chain

Assuming I should write to a binary. I can use the following code

val fos = new FileOutputStream("fileName") 

and then use

 fos.write(bytes) 

Is it always a good idea to combine it with a buffered stream? how in:

 val fos = new FileOutputStream("FileName") val bos = new BufferedOutputStream(fos) 

Does the same rule FileInputStream for FileInputStream ?

Do I need to close fos at the end (in the chain version)?

EDIT: Found the answer to the last question. There is no need to close internal threads as indicated here.

+4
source share
1 answer

Depends on the type of data you want to write. BufferedStream is intended to be used when you do not want the underlying system (the one that is actually recording) to be called for each byte written, while FileOutputStream is intended to be used when you want to write raw bytes, for example, when recording an image.

+3
source

All Articles