Java 7 introduces try-with-resources block in order to avoid resource leak and code blot in the finally block to close the resources you opened in the try block.
Example:
Example:
package com.bharat.io;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TryWithResourceExample
{
public static void main(String... args)
{
try (BufferedReader reader = new BufferedReader(new FileReader("C:\\test.txt")))
{
String line;
while((line = reader.readLine()) != null)
{
System.out.println(line);
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
No comments:
Post a Comment