InputStream and OutputStream in java
In this post , we will look at what is InputStream and OutputStream in java? In the previous post, we have seen I/O overview.Let’s understand them one by one.
InputStream class:
This is a parent class(base class) for all the Input stream in java I/O API. InputStream
Subclasses are like FileInputStream
, BufferedInputStream
and the PushbackInputStream etc.
In java, application uses InputStream to read data from sources like File, peripheral device, array and socket etc.
It reads the data in byte form, one byte at a time.
InputStream inputstream = new FileInputStream("D:\\MyData\\Sample.txt"); int data = inputstream.read(); while(data != -1) { //perform some task with data doSomeTasksWithData(data); data = inputstream.read(); } inputstream.close();
Explanation of above example.
Source: Our source is a file(Sample.txt) located at the path “D:\\MyData”.
FileInputStream: It is a subclass of InputStream.
read() method: read() method returns a int which contains byte value of byte read. InputStream has 2 more read() methods which can return byte array.
- int read(byte[])
- int read(byte[], int offset, int length)
End Of Stream: When it reaches end of stream, read() method will return -1. That means there is no more data to read.
Close() method: When there is no more data to read, we can close the InputStream by using Close() method and releases any system resources associated with the stream.
There are other methods as well in InputStream.
mark() method: It tells the current position in the Stream.
reset() method: If the input stream wants to go back to the point where mark was set by the mark method, then reset() will be called and InputStream can start reading the data again from that point resulting in returning data more than once.
markSupported() method: It checks if InputStream supports mark and reset methods.
Subclasses of InputStream shown below.
OutputStream Class:
The outputStream class is a superclass of all classes representing Output Streams of bytes i.e. subclassses are like BufferedOutputStream and the FileOutputStream etc.
An output stream accepts output bytes and sends them to some sink.
OutputStream output = new FileOutputStream("D:\\MyData\\SampleOutput.txt"); byte[] contentInBytes = content.getBytes(); output.write(contentInBytes); output.close();
Let’s understand the above code.
Destination: Our destination is a file(SampleOutput.txt) located at the path “D:\\MyData”, where the data will be written.
FileOutputStream: It is a subclass of OutputStream.
write(): Reading the content in byte array and then using write method to write into the OutputStream.
close() method: Closing the OutputStream after writing the data.
2 Responses
[…] previous post, we have seen InputStream and OutputStream. In this post, we will see how to read and write data with the help of FileReader and FileWriter […]
[…] previous post, we have seen FileWriter. In this post, we will see how to read data from a text file with the help […]