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