<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:media="http://search.yahoo.com/mrss/">

<channel>
	<title>file-handling Archives - Manish Sanger</title>
	<atom:link href="https://www.manishsanger.com/tag/file-handling/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.manishsanger.com/tag/file-handling/</link>
	<description>Java, J2EE, Spring Framework, application security,big data,database,design patterns,hibernate,j2ee,java,jdbc,micro-services,mongodb,multithreading,nosql</description>
	<lastBuildDate>Tue, 20 Mar 2018 08:23:33 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
	<item>
		<title>Java Try with Resources</title>
		<link>https://www.manishsanger.com/try-with-resources/</link>
					<comments>https://www.manishsanger.com/try-with-resources/#respond</comments>
		
		<dc:creator><![CDATA[manishsanger]]></dc:creator>
		<pubDate>Mon, 19 Feb 2018 11:50:16 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Java 7]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[try-with-resources]]></category>
		<category><![CDATA[file-handling]]></category>
		<guid isPermaLink="false">https://www.manishsanger.com/?p=320</guid>

					<description><![CDATA[<p>A resource is an object that must be closed once the program is finished or done using it, for example, file resource, JDBC database connection resource, socket connection resource etc. Prior to Java 7, there was no auto resource management, we open the resource in a try block and close the resource in finally block. [&#8230;]</p>
<p>The post <a href="https://www.manishsanger.com/try-with-resources/">Java Try with Resources</a> appeared first on <a href="https://www.manishsanger.com">Manish Sanger</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>A resource is an object that must be closed once the program is finished or done using it, for example, file resource, JDBC database connection resource, socket connection resource etc.</p>
<p>Prior to Java 7, there was no auto resource management, we open the resource in a try block and close the resource in finally block. For example:</p>
<pre><code class="java">try{<br />
//open resources<br />
}catch(IOException){<br />
//Exception handling<br />
}finally{<br />
//close resource<br />
}<br />
</code></pre>
<p>In the above approach there were few drawbacks:</p>
<ol>
<li>It causes memory leaks and performance issue if the developer forgot to close the resource.</li>
<li>Finally block will always be executed, it doesn&#8217;t matter if the exception is thrown from the try block. It means, it tries to close the resource in finally block, even if it doesn&#8217;t exist which again cause an exception to be thrown from finally block.</li>
<li>Exception thrown from finally block will be propagated up the call stack, though exception from try block is more relevant.</li>
</ol>
<p><b><u>try-with-resource</u></b><br />
In Java 7, a new approach called &#8220;try-with-resources&#8221; is introduced for auto resource management. In this when try block finishes, it automatically closes the resource.</p>
<p>Here is an example for try-with-resource construct:<br />
<pre><code class="java">private static void readFile() throws IOException {<br />
try(FileInputStream input = new FileInputStream("file.txt")) {<br />
int data = input.read();<br />
while(data != -1){<br />
System.out.print((char) data);<br />
data = input.read();<br />
}<br />
}<br />
}<br />
</code></pre>
<p>&nbsp;</p>
<p><b>try-with-resource using multiple resources</b><br />
We can use multiple resources in try-with-resource block, it will automatically close both the resources when program finish executing try block.</p>
<pre><code class="java">private static void readFile() throws IOException {<br />
try(  FileInputStream input = new FileInputStream("file.txt");<br />
BufferedInputStream bufferedInput = new BufferedInputStream(input)<br />
) {<br />
int data = bufferedInput.read();<br />
while(data != -1){<br />
System.out.print((char) data);<br />
data = bufferedInput.read();<br />
}<br />
}<br />
}<br />
</code></pre>
<p>The resources are closed in reverse order of they are declared inside the try. <i>BufferedInputStream</i> will be closed first, then <i>FileInputStream</i>.<br />
&nbsp;<br />
<b><u>AutoCloseable Custom implementation</u></b><br />
Java 7 has introduced an interface <b><i>java.lang.AutoCloseable</i></b>. To use any resource in try-with-resource block, it must implement the AutoCloseable interface. We can also implement <i>java.lang.AutoCloseable</i> interface in our own classes and use them with the try-with-resources construct.<br />
<i>AutoCloseable </i>interface only has one method class <i>close()</i>. Here is the <i>AutoCloseable</i> interface:</p>
<pre><code class="java">public interface AutoCloseable {<br />
public void close() throws Exception;<br />
}<br />
</code></pre>
<p>&nbsp;<br />
<b><u>Exception handling with try-with-resource:</u></b><br />
If one or more exceptions are thrown by try block in try-with-resources, the exceptions thrown are <b><i>suppressed exception</i></b>.<br />
Java added an constructor and two methods in <b>Throwable</b> class to deal with suppressed exceptions. We can get these exceptions by using the <b><i>getSuppress()</i></b> method of <b>Throwable</b> class.</p>
<p><b><u>Advantages of using try-with-resource construct:</u></b></p>
<ol>
<li>Auto resource management.</li>
<li>More readable code.</li>
<li>No need of finally block just to close the resource.</li>
<li>Meaning &amp; relevant exceptions.</li>
</ol>
<p>The post <a href="https://www.manishsanger.com/try-with-resources/">Java Try with Resources</a> appeared first on <a href="https://www.manishsanger.com">Manish Sanger</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.manishsanger.com/try-with-resources/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<enclosure url="https://www.manishsanger.com/wp-content/uploads/2018/02/Java-try-with-resource.png" length="34462" type="image/png"/><media:content url="https://www.manishsanger.com/wp-content/uploads/2018/02/Java-try-with-resource.png" width="757" height="232" medium="image" type="image/png"/>	</item>
	</channel>
</rss>
