Let's begin by looking at screen my Wall Street my wall reading and writing is handled by something 2
called streams and a stream is basically a connection between a program and a data source or sink under 3
just basically represented by a class and it is also specific to the type of source or sink. 4
For instance if source or sink is a file then we use a specific type of stream. 5
Similarly when dealing with network a different type of stream is used to work a stream can be either 6
an input stream or an output stream if you want to read some data from a source then you would use an 7
input stream. 8
Similarly if you want to write some bakeware destination then you would use an output stream and when 9
working with streams there are three operations that are involved Huston's to open a stream. 10
Second would be to read or write data depending on whether the open stream is input output. 11
That is if you are dealing with an input stream then we would read data from the input stream on and 12
off streams and conter But if we are dealing with an output stream then we simply write the door to 13
open output stream. 14
Finally we close the stream closing the stream would free up the system resources that the stream would 15
be using and this would help in awarding resources on system resources would be things like sockets 16
or file handles on the operating systems have limits on the number of sockets or file handles that can 17
be open. 18
So when we are done using a stream it is important to close it so that the associative system resource 19
will also be released. 20
Otherwise those resources will remain open and at some point we will not be able to create any new streams 21
not to cross the stream for sure we can close it inside a finally block. 22
And we wanted to discuss this when discussing finally block in the exceptions chapter. 23
We know that a finally block will always be executed regardless of whether or not there is an exception 24
. 25
And here's an example of a standard template for opening and closing streams. 26
And we looked at the specific goal when discussing try with resource a statement. 27
But let's discuss it one more time here. 28
The input stream is a file input stream. 29
I need the tools for reading data out from behind. 30
And one of the follow Plaisance even look at file input stream in more detail here within the try block 31
we are creating an instance of filing puts strain on that basically is nothing but opening the input 32
stream. 33
Let's not worry about the read operation but we will see later on here when we are creating the file 34
input stream object the constructor of the file input stream class can throw a file lock one exception 35
which we know is a checked exception and so we have this catch block to handle it. 36
The closing of the stream is done in the finally block by invoking the last method on the input stream 37
object. 38
No not is that within the finally block. 39
We have one more try catch block and thats because a close method can also throw an Iowa exception. 40
Moreover we are also checking that the reference variable in is not known if it is not null. 41
Only then we close the string not that variable in can be NULL if the final input stream object could 42
not be created. 43
That is a file not one exception was generated when we were trying to create file input stream object 44
on video distric we could get a null pointer exception when the Close method is invoked. 45
Now as you can see this whole finally block looks very ugly with the end try catch block under additional 46
logic so to avoid this we know that we can make use of the drive with resources statement which was 47
introduced in July 7 on here is the same code with private resources which is much cleaner. 48
The finally block we know that with this syntax the cleanup operation is taken care of. 49
Implicitly there is a close method is invoked implicitly and we know that in reality the compiler inserts 50
the time finally block into the bytecode. 51
So even if an exception is generated the Close method is still in work just as in the case of using 52
a finally block explicitly. 53
Just recall that the object in the parenthesis should be auto closer than that is it implements the 54
auto plausible interface or not the variable and is also implicitly final on this particular feature 55
is also referred to as Automattic resource management or shock. 56
Here is another example where multiple streams are being created within the Cripe parenthesis here the 57
stream creation experience must be separated by semi-colons one instance where you would be using multiple 58
streams is when we want to read data often on one stream and write it with a different stream. 59
That is you are doing some kind of a file transfer. 60
A lot of you will also see a table of such kind of a file transfer. 61
So if you know understand what streams are on how they can be opened and closed. 62
Next thing Binoy is that streams are off two types byte streams uncharacteristic is the reason for having 63
these two types of streams is because Joe differentiates between processing codirector data and everything 64
else who process character data like text classes that fall under characteristics are used on the process 65
non character data like Lexa images process underbite streams are used. 66
This record shows the base classes for both byte streams as well as character streams for instance input 67
stream is the biggest class for all byte oriented input streams. 68
One reader is the base class for all character oriented input streams. 69
So most of the stream I will classes that reading and writing will directly or indirectly subclassed 70
these base classes. 71
So that's an interaction between my will in the next couple of lessons. 72
We will focus on byte streams after which we would look at character issues. 73
Thank you