close

Java - Character Streams

we have already seen how you can perform

input and output operations over the by

data but now let us see if you have to

perform on character stream how do you

do that a character stream usually

support on 16-bit byte data and whereas

the byte stream allows you to perform

the input and output operations over

8-bit byte information and normally the

classes which are available to perform

input and output operation on these

character streams are file reader and

file writer although the file reader

uses file input stream internally and

file reader writer uses file output

stream internally to perform these input

and output operations but the main

difference between the pipe and the

character string where the file reader

and file input stream would be that the

character stream file reader and file

writer uses two bytes at a time so let

us do a thing let us understand these

with the help of an example how you can

write a data or how you can read a data

and how is it different from your byte

input and output string so in the first

example we are using the package Java

dot IO dot asterisk we are importing

this package because it includes all the

classes which are available to perform

these operation and in this class we are

trying to write certain data so let us

understand in the main method you have

written that it throws IO exception it

may and then in the bracket we will

write about the file write a class we

are using a file write a class to write

the data to our destination file and

this data may be of your character type

so initially you provide the value as

null and then you initialize the value

with your file name so you write as file

writer equal to space out is equal to

null and then out is equal to new file

writer and this in this file writer you

provide what is your text

and once you provide your text file name

you're saying that you would want to

write the data in it if you recall in

our previous videos where we used file

output stream to run a single byte then

we called a write method

provide the data in the bracket but if

you had to write the same message which

is called welcome to tutorials point in

that case you had to create an array of

bytes get each byte stored it in the

array of bytes and then so save it in

your destination file that is the

destination file would allow you to

store each byte at a time but where you

want to store the complete string at the

time in those cases you use file write

up where you can call the write method

of file writer class and directly write

the data so you call the object that is

the write method over these file writer

objects so you write out dot right in

the bracket you write the data what

needs to be written to your destination

file finally you need to close the

connection again you have a close method

to close the resources and all so you

need to call over your file writer plus

and that is out dot close so now when

you have successfully written the block

of statements which will execute to

write the data to a destination file

provided then you need to catch if there

is an exception and print that exception

so let us execute this program

practically to understand how you use a

file writer to write the stream of

information over your destination file

so let us now see how you can write to a

file using a file writer early we have

used file output stream to write certain

bytes to a fight but when you want to

write certain characters or a text then

it's good practice or recommended to use

file writer we have created a class

called copy file in that we have written

our main method in that main method we

use the file writer class initially we

have provided the value to this file

writer class as null and now

in the try block we shall create its

constructor that is we should actually

create the object that is out there is

nothing but your file writer object so

you write out is equal to new file

writer and you need to provide the file

in which you would want to write the

data now we say that we would want to

write a particular data one way is that

what we have alia see you can declare

available and then write this particular

data in your out file this is one way

the other way is directly writing the

data in your write method so you do have

a write method for your file write a

class which enables you to write the

data to your file we have written

welcome to tutorials point so now this

can dot txt file should hold the

information called welcome to tudo

tutorials point once you execute the

program and once on execution

successfully it will print SMSs success

and then we need to close the file

writer class and then if there is an

exception it will be cached by your

catch block which is your exception

handler so let us execute this program

it says success now we shall go back to

eat drive files and then car dot txt

file

so now you see it has written

welcome to tutorials point so this is

one way of writing and the other one was

that we have already seen by using file

output stream so one way to write

character data is file output stream but

file output stream is mainly used to

write the bytes

whereas file writer helps you to write

number of data that you would want to

write so in this example let us now see

how do you use a file reader class to

read the string or the characters of

information and then display on the

output so we have imported the package

Java dot IO dot asterisk which comes

includes or consists of all the classes

which may be useful to run your this

particular file right so we have created

a class called read file in the method

we say it may throw an i/o exception and

then you write try cashflow initially

you have initialized the file reader

class with a null value later with the

help of a new object a new keyword you

create the object of this particular

class and then in the constructor

parameters you provide the value of your

file that is your filename in which or

from which you are trying to read the

data so we have written in is equal to

new file reader where in is your file

reader class object with the help of new

keyword you are creating this object in

the constructor you have provided the

file from where you are trying to read

the data and then once now you know from

where you have to read the data you need

to call a read method over this object

which consists of the file in order to

read the data and then display similarly

like what we have done in file input

stream we have taken or used a while

loop to read the data similarly we do it

here here we take it as in dot read you

store it in a variable and until unless

it is minus one you keep reading that

and in this you don't have to convert

because it will directly be display and

then finally

you close this file and to close the

class you use a closed method that you

call over your file greater class and

now once you know that you have

successfully able to read the data and

then you have to close the connection

and once it is done if there is an

exception it will be cached in your

exception class right and this exception

handle will hold this kind of an

exception and if you want to know again

I am repeating if you want to know what

kind of an exception and where it has

occurred you would use a print stack

trace method so let us execute this

program practically to understand in

detail how you can read the data from a

file using a file reader class this is

an example of file reader class which

helps me to read the set of characters

cause string of data from a file and

then display here we had already done

this using file input stream which

helped us to read the number of bytes

however we can also read the characters

using file input stream but it is

recommended to use file reader if you

would want to read number of text so

here we have created a class called read

file and under the main method we will

create a file reader class which will

hold the information about the file so

we have written file reader which is

your class name and then the object

equals to null initially we have

initialized the value of file reader

class to null and then in the try block

we are trying to provide the information

that is we are trying to initialize the

constructor with the value that is we

write in equals to new file reader and

then the file name and if you think this

is confusing you can also remove this

from your and then directly declare it

here you do not have to make it twice

you don't have to initialize the value

as null you can write it in this way and

the other way was to initialize the

value initially as none and then provide

the value so now the file reader shall

read this file and now in order to read

file we need a integer right the

it will read the number and then convert

this integer into a character format we

use a while loop because we want to read

a set of characters available in that

fight if it was a single character we

would have directly read and know why

look was needed but there are n number

of data so we have to use while loop

then in while loop we are trying to read

this file which is I am so I am dot

three trade is the methods on this file

reader class and then you are trying to

store the value in I Anton unless it is

minus one that is until unless the whole

file is right and when you are reading

the file and store it storing it in I

please display I and we have cascaded

with can because we want to display the

data in character format and not integer

form right once it is done we need to

close this particular file and then that

is after you close your value you are

trying to close the file reader class

and then if there is an exception it

will call them so let us execute this

file and it says welcome to tutorials

point so this is how you use file reader

and writer to read or write the

characters from or two or five