FileInputStream and FileOutputStream in java

In the previous post, We have seen FileReader which is used to read the data from text file. In this post, we will look at FileInputStream and FileOutputStream in java. These classes are used to read and write the data to a file i.e. used for file handling.

Let’s look at them with example.

FileOutputStream:

It is used to write the data as a stream of bytes into a file. The FileOutputStream class is a subclass of OutputStream meaning you can use a FileOutputStream as an OutputStream.

For writing character oriented data, we can use FileWriter.

Example of FileOutputStream:

Output: Write tasks successful.

FileInputStream:

It reads the content of File as a stream of bytes. It is a subclass of InputStream. For reading the stream of characters, we use FileReader class.

It can be used to read byte-oriented data for example to read image, audio, video etc.

Example of FileInputStream:

Output: I am making a painting.
Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum

 

Print Friendly, PDF & Email
FileReader in java
BufferedInputStream and BufferedOutputStream in java
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

1 Response

  1. August 8, 2015

    […] the previous post, we have seen FileInputStream and FileOutputStream. In this post, we will look at BufferedInputStream and BufferedOutputStream in […]