close

Java - Input & Output Streams

my now we know what are the input and

output strings in Java and why are they

useful so let us learn in detail about

these input and output streams and what

are the type of classes available inside

the input and output streams so as you

see here it talks about what is an input

stream and what is an output stream then

what other classes are the subclasses of

these input and output streams whereby

now we know that an input stream is used

to read the data from the source file

and output stream is used to write the

data to your destination file so here

the object has two input and output

stream class a input stream class you

have five input stream

byte array input stream filter input

stream object input stream and similarly

in output stream again you have your

output stream so let us talk about one

of them it means the same light input

stream would be helpful to use to read

the data and any of these output strings

are used to write the data so let us

talk about the file output stream file

output streams helps you to write any

data to your destination that is a file

output stream is mainly used to write

byte data so you have the other types

also like normally a data can be of your

primitive type your character type your

object ID so now what each plus of what

each class of these support what kind of

a data we need to understand that so the

first one is fine output and file input

stream where these classes are useful to

read or write your byte inputs and that

is byte data which may be of a single

byte right a bit by that is normally

used for your file output stream next is

your byte array output stream and what

does this byte array or input stream

does is that it allows you to read or

write data which can be written to

multiple files so byte array output

stream allows you to write data to

multiple files together and what kind of

a data will be written together the byte

data can be written in

and number of files together the next is

filter outputs right which is then

subclass into data output stream and

buffered output stream what does this

data output stream does data output

streams suppose most of the primitive

data types and it is mainly used so that

the data can be written in future so

that the data can be used or referred in

future whereas in file output stream and

byte array we immediately write them

data and whereas in case of data output

stream these are used so that you can go

through in your future and it supports

the primitive data types the next one is

buffered output stream the buffered

output stream is mainly used to improve

the performance to make the performance

faster when in comparison to the rest of

the output stream and how it makes the

faster process by storing the data in

buffer and then from the buffer the data

is written in your destination file then

it will be moved to your output stream

normally in other cases the data is

directly written to your output stream

that is to your destination whereas in

case of buffer output stream the data

will be stored in a buffer and then with

the help of a flash method you write the

data from the buffer output stream to

the main output stream next is your

object output stream

what are these object output stream and

object output stream almost supports all

the primitive data dyes and the graphs

of Java objects to be written to your

output stream again repeating it suppose

all the primitive types and graphs of

the type of Java objects that is written

to your output stream and these of this

should support Java dot IO dot

serializable interface otherwise you

will not be able to work so today in now

we will see in the video is that what an

input and output stream like you have

all these input streams where you can go

to the tutorials point and learn in

detail about each of them however in the

going forward videos we shall learn

about these input and output strengths

how you can use to write a byte data and

rate

we'll see how you can use to write a

character data here it talks about what

is your file input stream

normally in input and output stream the

most type of class which is being used

as your final input and file output

stream where final input streams allows

you to read the data from the source

file that is a byte format which allows

you to read the by you can use file

input stream to use your character

datatypes also you can read from the

source file which is of character data

but it is recommendable to use file

reader than file input stream so file

input streams are mainly used to read

the data from source file which is of by

format then how do you write these let

us see that we write input stream we

create an object equals to new file

input stream and then the file name let

me explain this as you know file input

stream is a subclass of input stream

type right so what happened you can

either write directly five input string

you created object and you create the

object with the help of a new keyword

this new keyword helps you to create the

object and then you neutralize the value

in the constructor right so this is your

final push stream which is a constructor

to your input stream class so in this

you can directly provide the file name

from where you are trying to read the

data for example I say I have a file

name called welcome and in that I have a

text call welcome to tutorials point I

want to read that data and display it in

the output in that cases we use file

input stream to read the data the next

it says that how you can create a file

and then read the file one way is that

you directly read the file next how you

read a file we need to see that we have

used a file object yet equal to new file

and you have provided the file name in

your file object now this file object is

provided as a parameter to the

constructor to work with your input

stream object so either you can directly

write like this or you can write create

a file object and then

a file object in the constructor

parameters to create your input stream

object we have certain methods which

enable to use file input stream normally

when you use file input stream why do

you use you want to read the data or you

want to read number of bytes when you

want to read only a single by using this

read but if you have number of bytes to

be written then you use your the read

bytes right so which is nothing but the

length of your bite and then you have

certain other methods which is close now

when you use file input stream when you

are opening the file to read the data it

is always a good practice to close that

data and how would you close with the

help of your close method and close

method will close all the resources

linkages that you have been using and if

you want to close the connection then

you use the finalize method and then we

have final output stream it is similarly

but just opposite where input stream was

useful to read the data and output

stream is useful to write the data to

some destination file the syntax follows

the same you write output stream which

is a class F which is an object and this

object is created with the help of new

keyword followed with the constructor

name that is fine output 3 and you

provide your file name right the other

way would be again you create your file

object equals to new file you provide

the complete string this is your

complete path the complete string and

then this path has been stored in F

object this will be passed in the

constructor or file output stream which

will create an output 3 object both this

input and output streams are also used

in case of byte array let us now see few

of the method is so similar if you want

like you have opened a connection to get

the information right to retrieve the

information now when you want to store

the information you are again calling a

class which is an output string so you

have to also close the connection to

this output stream because there are

certain resources that you have been

using and if you do not flow

those connection or if you do not leave

the linkage to those resources it will

be occupied and the other object which

is in the need of these resources will

have to wait so it is always good

practice to close whatever resources

that you have been using and with the

help of finalize method finally you

close the connection to your destination

file and then write methods are normally

used to write the data to your file it

says that byte array output stream and

detail output stream are also they use

output stream class in order to write

the data so we do a thing let us learn

in detail about each of this input and

output stream to see what else do you

have further how you can work then

practically in going forward videos we

shall learn about how you can read or

write a byte data which may be a single

character or string of characters and

then we shall see what are the tactic

losses what are the classes that is

useful to read or write the character

type and then how you can navigate

through the files for how you can create

the directories