close

Java - Byte Stream

so by now we know what input stream and

output stream does and how you can read

the data what kind of a data do you read

so in this video we shall learn about

how you can read or write your byte

stream data normally a byte stream data

it performs an operation of input and

output which is of 8-bit bytes right so

it performed the operation on 8-bit byte

data which may be an input performance

or it may be an output operation so let

us see normally you have n number of

classes available to perform these

operations you have byte array you have

data but in this we are just using an

example of five input and output which

is the most commonly used one so we have

created a class called copy file but

before that we have imported a package

called Java dot IO dot asterisk why are

we using this package because this is

the package which is providing us the

classes for input stream and output

string where file input stream and file

output stream are then the subclass of

input string and output stream and these

are provided by your i/o package you can

also extend by writing Java dot IO dot

IO file input stream or I or dot file

output a and then the close man asterisk

denotes them whatever classes are

available in this package can be

available for this particular class so

we have created a class called copy file

under that you have a string the main

method as we have known that in i/o

normally there are certain kind of an

exception that may occur it may be a

file not found exception

it may be an i/o exception or any kind

of an exception so it is always

recommended to use the try catch block

or the exception handler to handle if

there are any exceptions so in the going

forward chapters also wish learn to

create the program with the help of your

try catch block so in the string of we

throws IO exception normally what is an

IO exception how you write an IO

exception will learn but now if you have

to cast the exception you write in your

try cache block or you can write what

kind of an

suction your program may throw and

you're right throws are you exception

where throws is a keyword and i/o

exception is a type of an exception then

in the braces we need to provide what is

your try and finally block so here we

have seen that we've been trying to read

a file with the help of file input

stream and then we'll write to now the

file with the help of file output string

so we have used the key word that is our

class name file input stream we have

created the object and initially the

value for file input and find output

stream is set to null so initially when

we are using the file input and file

output stream we have said to null

however if you do not want to set to

null and you know the value you can

directly provide in your try block we

have just provided for now the value for

both file input stream and output stream

is null we have not initialized any file

value to fit then in the trial block we

write those block of statements where we

expect or anticipate this kind of an

exception may occur so we have used the

object in equals to new file input

stream when you use the new keyword it

means you creating an object for your

file input stream and then followed with

the constructor name you can also write

it as input stream in equal to none and

then you initializes new file input

stream it will not make difference

whether you write file input stream or

input stream because file input stream

is a subclass of your input string the

next you provide what kind of a file it

is it is dot dot txt file normally we

are using dot txt file you can use any

other file so we have two files one from

where we are taking input then they have

another txt file where we want to write

the output so the first file name is

input dot txt nice in our output stream

like you know now from where you have to

read the data next is then where do you

have to write the data so you write out

is equal to new file output stream in

the bracket you provide your file name

that is output dot txt now you know from

where to read the data and from where to

the data but what are the steps what are

the block of statements that you need to

execute to perform these operations that

is a program is where you write the

block of statements which will allow you

to read the data from a file and write

the data to another file so what you

have to do in order to read a single

byte you can just say in dot read and

then you are displaying it or you are

storing it in your or you are writing

into your output method to read the data

from an input stream you use a method

called read and to write the data you

use a write method so let us see because

there may be n number of data in that

particular file you may need to read n

number of Dwight so we use a while loop

where it says in da tree that is this

object in da tree and keep reading until

unless it is minus 1 until unless you

meet the last point until unless you

read the last byte okay

and when you read you need to store it

somewhere right and when you store the

information normally you get the data in

numeric form that is you get the data in

bite form and now when you're getting an

invite form you need to store it

somewhere and it's open the integer type

you normally get the ASCII characters

those ASCII characters has some certain

values integer value so you are storing

it in an integer

therefore you have defined an end

variable int C so C is equal to in da

tree open a closed bracket and this hole

needs to be written in one bracket

because we are saying if this is not

equal to minus one then you read so this

whole equation until it goes negative

keep reading it and then you need to

display then we used we need to use

system dot out dot println and C but

here we are not displaying what are we

doing we are trying to read from a file

and then we are trying to write into a

file so from this statement by now we

know how to read the data from a file

but now when you write the data you say

this particular object which is your au

which has this particular file so you

write out dot write method

and in that write method you provide the

variable in which the data is stored so

for now the data will be stored in C so

this loop keeps rip repeating until

unless the condition meets fall so it

will go first for an example you're

trying to read the data as welcome to

tutorials point so first it will fit as

W store it and see and write it to this

particular file output dot txt then

again it will go to this input dot txt

file that is in dot V then E and then it

will store it in C and then write it to

your output dot file so it will keep

repeating until unless if you need cell

last by until unless your file was

negative so you need to close this try

block because this is the block where

you're suspecting a kind of an exception

may occur normally if you're not

throwing an exception here you need to

immediately follow with a catch but here

we have already written so we do not

need to write a catch block again so

finally block is always written with at

least one try block if there is no try

block no finally block can be written

and finally block says that whether you

receive an exception or not you need to

execute these statements and in these

statements what are we saying we are

trying to close the connection of our

input stream and output stream has

already discussed it is always

recommended to close because the

resources may be used by any other

object so in finally block when you open

you say if in is not equal to null that

is until unless your this is not equal

to null then you need to come and close

and similarly if out is not equal to

none please come and close so this is

how you read a data from a file and

write a data from to another file and

this is in case of your byte so in the

going forward videos we shall learn how

do you read and write a string where or

how do you read and write a character