Byte Streams in Java

In java programming, to perform input and output of 8-bit, Byte stream is used.

Most common byte streams classes are, FileInputStream and FileOutputStream.

FileInputStream : FileInputStream is used for reads one byte at a time .

FileOutputStream : FileOutputStream is used for writes one byte at a time

Writing Bytes

Example: Write a program, to write a bytes to a  file using Byte stream class.

Result

checking file A.txt it will show following content.

Note: If there is no file A then, file A will be create automatically

Reading bytes from a file

Suppose we have a file “A.txt”

Hello Students I am Aditya

Example: Write a program, to read a bytes from file A using Byte stream class and print on output screen.

Example: Write a program, to copy the content of file A into file B using Byte stream class.

Note: if there is only file A in which data is written and no file B then, file B will be create automatically.

Suppose we have a File “A”

Hello students I am aditya.  

Output: When we run above program one new file create with Name “B” and content of file “A” will be copy into file “B”.

File “B”

Hello students I am aditya.