Java Complete Tutorial Ep. 56 - Automatically Closing File Streams (Try-With-Resources)

hey what's up guys back again with

another video in the Joe okay guys so

I'm back and this is gonna be a short

episode because there's not much we have

to learn here but this is also pretty

cool so I'm gonna show you it some

automatically closing file streams in

which you you know you make a file

stream by declaring a new object of for

example file input stream and then once

it's done it will automatically close so

you don't have to call the doc closed

method or the close method so yeah so

the way that we do this is we use it

with the try statement so I'll show you

first let's just go ahead and make a

file stream file input stream like we

normally would so let's do that so file

I mean well let's see here

yeah we're just a fair one so file input

stream oops file input stream thin is

equal to you file interest stream okay

inside of that we'll just go ahead and

create a file which have a file here

we'll call it


things txt okay it doesn't really matter

what you call it and we'll just put some

stuff in here like hmm I like trains do

you like trains okay so pretty simple

and then so we'll do things to XT as our

thing here and of course we have to

surround this by a try in catch

statement because this will throw an

exception so we have to handle the

exception just in case it happens so i/o

exception and then we're output so

basically we have a new we establish a

stream input stream between this file

here and our program okay so now that

acts is it of course so we could do

we'll just have a loop where we can read

everything and just output it again so

basically like this so do-while oops

didn't mean to do that

do-while and so we need a variable so

just do int input and then input is

equal to fin dot read so just read a

single byte and then so this will and

then we'll output input

once it's read and stored inside input

will just output it so then we'll do

that we'll loop through this through the

whole file as long as input is not equal

to negative one okay because negative

one is and it means then the file of

course so yeah I mean so that's the end

of the program pretty simple little

program let's go and run it hopefully

should just print out everything inside

of our file here okay and it prints it

out as numbers so we need to cast it if

you want to see what the letters are

okay let's go and do that boom so we get

everything I like trans do you like


okay cool so that's how you do that but

we forgot to add something basically if

we if we leave this open basically um

and we in the pool the program stops so

it's not a problem but basically if we

have a program running and it keeps

running even though we're not you know

accessing this fast

anymore we're done using it it's still

gonna still gonna have that safe space

that that that's piece of memory saved

for this file input stream okay so

you're wasting memory and you have a

memory link you could have a memory leak

that could happen so basically what I'm

trying to say is that you want to close

this stream once you're done using it by

using fin close so we'll just go ahead

and add that just to test it out and

yeah so there you go and now we've

already oh we've already done all of

this in the past episodes but basically

what I want to show you is that we don't

actually need to type this manually

anymore I believe either JDK six or

seven they added a feature where you can

actually automatically close it within a

try statement so if we add a try

statement here and give it a parameter

the parameter will actually be our

stream okay so if we just cut this and

put it in here and then get rid of this

semicolon and okay so says try with

resources are not supported at language

level six so yeah as you can see it was

established in JDK 7 so basically what

this means is that in this development

environment right now we have it set to

Java level six which is basically the

sixth version of Java so we need to go

ahead and switch it in our project

structure to Java level whatever you

want to as long as it's not six

everything above six I mean so I'm gonna

switch it to ten because we're actually

using a JDK 10 so might as well use the

latest version anyway so yeah I'm just

going to switch the latest version and

now the error goes away and we can do

this now so if we go ahead and run this

it actually works

so yeah everything works the same it

closed it because basically the way this

works is it makes this try statement and

then once it's done running you know it

knows it's done running once it's done

so it'll just automatically close this

stream for us okay so it's really

magical and yeah so it's very simple and

this is called try with resources by the

way this is just the method that's the

method that we're using to do this is

called try with resources so yeah so

just before we in this episode I'm going

to show you how to do it with multiple

streams like last episode we deal with

the file input stream and the file

output stream so how would we establish

this for both streams at the same time

well we can do that

by separating them by semicolon so let

me show you if we copy the code from

last episode let me just copy it here

copy okay paste so be copied from last

episode just ignore the comments by the

way we can actually do the same thing so

if we add a parameter to our try

statement here and then we just let's

get rid of this okay so if we cut this

first one the file output stream and put

in here now the thing down here will

gray out because it's redundant so we

can actually keep it here there won't be

an error but this is useless because

it's already going to close by itself

anyway because you know it's up here

it's a try with resource so it's gonna

close by itself we don't even need this

anymore but this one we still need

because we haven't added this one yet so

we're gonna go ahead add this one so cut

it I'm at a semicolon here because you

have to separate them by semicolon when

you're using more than one and then

delete the semicolon on the end because

you don't need that anymore

okay so we'll get rid of this awesome so

now we can do that so let's go and run

this we just got an error because this

program uses a file called lyrics txt

but we don't have a lyrics txt inside of

this file inside of this file structure

because we just copied it so anyway the

point is it works this is how you add

multiple streams inside of your your try

statement okay so hopefully you liked

this episode if you have any questions

at all just leave a comment and help you

also we have a discord that's in the

description if you wanna go ahead and

join there's a link in the description

also all of the code from today's

episode is actually there's a link for

it in the description where you could

check it out you can bookmark it use it

as a reference whatever you want to do

for the future because sometimes you

forget code sometimes I forget code it

happens and sometimes you just forget

how to do things you know so that's okay

so make sure you do that check it out

yeah so if you liked the video leave a

like if you see more subscribe and peace