Check out a free preview of the full Java Fundamentals course

The "Closing the File Reader" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie walks through updating the previously created file reader program to close when an exception is thrown.

Preview
Close

Transcript from the "Closing the File Reader" Lesson

[00:00:00]
>> We are going to look at the program that we've just written to close the fileReader. So we open Scanner, we did not close it, right? So remember, we wanna close our streams, especially when you're dealing with files. Because when you open a file like the program does, you've all seen this before where you try to manipulate some file open and some other application has this locked, right?

[00:00:28]
So you got this locked until you let it go. So, we want to go and close the fileReader. Now this part is kind of tricky is like, where should I close it? If I were just looking at this on instinct, then I would probably want to close it like after this while, because now I'm done reading from the file, right?

[00:00:57]
So let's say fileReader.close, and that's fine but what if there was an exception thrown, maybe while we were reading from the file that actually did happen, right? When we had that string. So if an exception is thrown here on line 15 that input mismatch exception, it's going to go inside of the input mismatch exception catch block on line 21, it never ever executes line 17.

[00:01:36]
So I wouldn't close it, right? All right, so I think well, crap, I don't wanna do that. What if I moved it inside of here, or inside of the other exception? Well, now I can't even access it cuz I'm outside of the scope, but even if I were to move scan reader outside of the try block, let's do that.

[00:02:01]
In fact, I'm gonna move this. I'm gonna just set it to null to begin with, and then we'll set it in the try, all right? So if I move it to here, like if this one is not thrown then this never gets close, so this doesn't feel like a good place to put it either.

[00:02:26]
So it's like, okay, what do I do? Now, there's another clause that you can add to these toe try catch block is called finally. And finally just has a set of parentheses, I mean not parentheses, curly braces, and you will add any statements that you want executed after this try catch block, no matter what happens.

[00:02:53]
If you stay in the try, if an exception is caught or whatever, I don't care, after you're done with this whole thing, this finally block will say, well, go ahead and do these statements. So this would be a good place to put that. We could say fileReader.close right here.

[00:03:11]
So no matter what happens, finally I want you to close that thing for me.
>> How does the finally and the curly braces work with regards to the scope of that try statement?
>> Yeah, so if I were to leave this here, I don't think it would know that.

[00:03:28]
Yeah, it wouldn't, cuz even the catch blocks didn't know fileReader when it was here. Remember, when we declare a variable is only known within the curly braces that it's defined, so when we defined it here, you can't use it and catch and you wouldn't be able to use it and finally either.

[00:03:47]
Which is why we needed to move it inside of the curly braces of the method, so that you can access it within these smaller scopes. So when resources such as input or output streams are open, and an exception interrupts the flow of the program, again, those resources aren't closed properly unless you took specific precaution to do so.

[00:04:16]
So we've already talked about the finally part, right? So there's that and that's perfectly fine, but there's also a better way that you can handle this with a little bit less code. So that's with try with resources. Try with resources is gonna allow you to declare resources that you'll be used within the try block.

[00:04:41]
And then Java will automatically close those resources for you after the execution of the block. So that's pretty nice. So we're going to update our code, and we're gonna automatically close fileReader within the try block itself. So I'm gonna comment this out just so you have this as an example, okay?

[00:05:06]
Is that correct?
>> You still have the exception for E.
>> All right, so, the way we would do this, this is the only time that try has a set of parentheses. Otherwise, try just has the curly braces. But then within parentheses you can declare, like any resources that you want automatically closed at the end of this thing.

[00:05:32]
So I would actually define fileReader here. So I'm gonna take the fileReader. Man, okay, I want you to have both of these examples. I'm just gonna copy this. Shoo, and I got a main. [LAUGH]
>> Refactor into a new method, really good to just refactor it into a new method.

[00:06:00]
>> No, I'm gonna make, I'm gonna copy this class, I'm gonna copy this class. All right, let's clone it. So I'm gonna copy, we're gonna make a paste, and we'll call this one AutoClosable. No, what is it called? TryWithResources, let's just call it that. TryWithResources. Okay, So, we can get rid of finally, yes.

[00:06:53]
And then, with the try, we will declare our resource within the parentheses, and you can go ahead and actually assign the value, too. And we'll clean this up, and that's it. So basically, I just put the declaration inside of a set of parentheses next to the try, and that has declared this as a resource that I would like closed after we're done with this block.

[00:07:30]
Now the only types of resources you can put in here, you can't put anything you want. They have to be a subclass of the AutoClosable class within Java. Scanner happens to be one of those, so we're in luck.
>> Can you put more than one resource or just one?

[00:07:48]
>> You can. So if you wanted to put another resource, see, I think I might have an example. Yeah, I do. Give me a moment. All right, so I wanna combine these exceptions, too, so you have an example of that. So we'll see, or let's get rid of that so nice and clean, all right.

[00:08:23]
Oops, getMessage. All right, well, that looks nice.
>> Clean, readable.
>> You see? All right.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now