Java Reference
In-Depth Information
In the program, pay special attention to how the file is opened within the
try
-with-re-
sources statement:
Notice how the resource-specification portion of the
try
declares a
FileInputStream
called
fin
, which is then assigned a reference to the file opened by its constructor. Thus, in this
version of the program the variable
fin
is local to the
try
block, being created when the
try
is entered. When the
try
is exited, the file associated with
fin
is automatically closed by
an implicit call to
close( )
. You don't need to call
close( )
explicitly, which means that you
can't forget to close the file. This is a key advantage of automatic resource management.