- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Automatically Close File
In the previous chapter, the example programs have made an explicit calls to close() to close a file once it is no longer needed.
As discussed, this is the way files were closed when using versions of Java prior to JDK 7. Although this approach is however valid and useful, JDK 7 added a new feature that offers another way to handle resources, such as file streams, by automating the closing procedure. This feature, sometimes referred to as automatic resource management (ARM), is based on an expanded version of the try statement.
Automatic Resource Management in Java
The main advantage of automatic resource management is that it prevents the situations in which a file (or other resource) is unknowingly not released after it is no longer needed.
As explained, forgetting to close a file can result in memory leaks, and could lead to the other problems.
Automatic resource management is based on an expanded form of the try statement. Below is its general form:
try(resource-specification) { // use the resource }
Here, resource-specification is a statement, declares and initializes a resource, such as a file stream. It consists of a variable declaration in which the variable is initialized with a reference to the object being managed. When the try block ends, the resource is automatically released. In case of a file, this means that the file is automatically closed. Therefore, there is no need to call the close() explicitly. Of course, this form of try can also include catch and finally clauses. This new form of the try is called the try-with-resources statement.
The Java try-with-resources statement can be used only with those resources that implement the AutoCloseable interface defined by the package java.lang. This interface defines the close() method.
The AutoCloseable is inherited by the Closeable interface in the package java.io. Both interfaces are implemented by the stream classes. Thus, try-with-resources can be used when working with streams, including file streams.
Java Automatically Close a File Example
As a first example of automatically closing a file, here is a reworked version of the FileOperationDemo (shown in previous chapter) program that uses it:
/* Java Program Example - Java Automatically Closing a File * This version of FileOperationDemo program uses a * try-with-resources statement to automatically close a * file after/when it is no longer needed * * Remember, this code requires the JDK 7 or later */ import java.io.*; class FileOperationDemo { public static void main(String args[]) { int i; /* first, confirm that a filename has been specified */ if(args.length != 1) { System.out.println("Usage : FileOperationDemo filename"); return; } /* the following code uses a try-with-resources statement * to open a file and then automatically close it when * the try block is left. */ try(FileInputStream fin = new FileInputStream(args[0])) { do { i = fin.read(); if(i != -1) System.out.print((char) i); } while(i != -1); } catch(FileNotFoundException e) { System.out.println("File Not Found..!!"); } catch(IOException e) { System.out.println("An I/O Error Occurred..!!"); } } }
In this program, pay special attention to how the file is opened within the try statement:
try(FileInputStream fin = new FileInputStream(args[0])) {
Notice here, how the resource-specification portion of the try declares a FileInputStream called fin, which is assigned the reference to the file opened by its constructor. Therefore, in this version of program, the variable fin is local to the try block only, being created when the try is entered. When the try is left, the stream associated with the fin is automatically closed by an implicit call to the close(). You don't need to call the method close() explicitly, means that you cannot forget to close the file. This is the key advantage of using the try-with-resources.
It is important to realize that the resource declared in the try statement is implicitly final. This means that you can't assign to the resource after it has been created. Also, the scope of the resource is limited to the try-with-resources statement.
You can handle more than one resource within a single try statement. To do so, simply separate each resource specification with a semicolon.
Here this program shows an example. It reworks the FileOperationDemo (for copy file, shown in earlier chapter) program so that it uses single try-with-resources statement to manage both fin and fout.
/* Java Program Example - Java Automatically Close a File * A version of file FileOperationDemo (to copy file) that uses * the try-with-resources * This illustrates the two resources (in this case files) * being managed by single try statement */ import java.io.*; class FileOperationDemo { public static void main(String args[]) { int i; /* first, confirm that both the files have been specified */ if(args.length != 2) { System.out.println("Usage : FileOperationDemo from to"); return; } /* open and manage the two files via the try statement */ try(FileInputStream fin = new FileInputStream(args[0]); FileOutputStream fout = new FileOutputStream(args[1])) { do { i = fin.read(); if(i != -1) fout.write(i); } while(i != -1); } catch(IOException e) { System.out.println("I/O Error : " + e); } } }
In this Java program, notice how the input and output files are opened within the try block:
try(FileInputStream fin = new FileInputStream(args[0]); FileOutputStream fout = new FileOutputStream(args[1])) { // ...
After this try block ends, the fin and fout both will have been closed. If you compare this version of the program to the old version, you will see that it is much shorter. The ability to streamline source code is a side-benefit of automatic resource management.
More Examples
Here are some examples related to files in Java, you can go for.
- Read File in Java
- Write to File Java
- Read & Display File in Java
- Copy File in Java
- Merge two Files in Java
- List files in a Directory Java
- Delete File in Java
« Previous Tutorial Next Tutorial »