Java File Streams

aimtocode School

File Stream is used to read and write binary data to a file using File InputStream and FileOutputStream.

FileInputStream

Java FileInputStream class obtains input bytes from a file. It is used for reading byte-oriented data (streams of raw bytes) such as image data, audio, video etc. You can also read character-stream data. But, for reading streams of characters, it is recommended to use FileReader class.

The Java FileInputStream class makes it possible to read the contents of a file as a stream of bytes

example 1:



Output :

Available bytes in file: 48
Hello world.
This is a FileInputStream Program.

example 2:



Output :

Available bytes in file: 48
Hello world.
This is a FileInputStream Program.
------After finalize called--------
java.io.IOException: Stream Closed
  at java.io.FileInputStream.available(Native Method)
  at com.journaldev.examples.FileInputStreamFinalizeExample.main(FileInputStreamFinalizeExample.java:31)
aimtocode School