Why You Need to Close the Java Streams in Finally Block? Last Updated : 27 Oct, 2021 Comments Improve Suggest changes Like Article Like Report In Java finally block is a block used to execute important and common code. The finally block is mostly used during exception handling with try and catch to close streams and files. The code in finally block is executed irrespective of whether there is an exception or not. This ensures that all the opened files are properly closed and all the running threads are properly terminated. So, the data in the files will not be corrupted and the user is on the safe-side. Finally block is executed after try and catch block. The flow of the code is: trycatchfinally Below mentioned are two examples of when exception is caused and when it is not caused. We will observe that finally block is executed in both our codes. Example 1: With exception in code Java // Java program to show the execution of the code // when exception is caused import java.io.*; class GFG { public static void main(String[] args) { try { // open files System.out.println("Open files"); // do some processing int a = 45; int b = 0; // dividing by 0 to get an exception int div = a / b; System.out.println("After dividing a and b ans is " + div); } catch (ArithmeticException ae) { System.out.println("exception caught"); // display exception details System.out.println(ae); } finally { System.out.println("Inside finally block"); // close the files irrespective of any exception System.out.println("Close files"); } } } OutputOpen files exception caught java.lang.ArithmeticException: / by zero Inside finally block Close files Example 2: Without exception Java // Java program to show the execution of the code // when exception is not caused import java.io.*; class GFG { public static void main(String[] args) { try { // open files System.out.println("Open files"); // do some processing int a = 45; int b = 5; int div = a / b; System.out.println("After dividing a and b ans is " + div); } catch (ArithmeticException ae) { System.out.println("exception caught"); // display exception details System.out.println(ae); } finally { System.out.println("Inside finally block"); // close the files irrespective of any exception System.out.println("Close files"); } } } OutputOpen files After dividing a and b ans is 9 Inside finally block Close files Comment More infoAdvertise with us Next Article Why You Need to Close the Java Streams in Finally Block? shubhigupta22 Follow Improve Article Tags : Java java-stream Practice Tags : Java Similar Reads Java program to merge two files into a third file Prerequisite : PrintWriter , BufferedReader Let the given two files be file1.txt and file2.txt. Our Task is to merge both files into third file say file3.txt. The following are steps to merge. Create PrintWriter object for file3.txt Open BufferedReader for file1.txt Run a loop to copy each line of f 2 min read PrintStream close() method in Java with Examples The close() method of PrintStream Class in Java is used to close the stream. Closing a stream deallocates any value in it or any resources associated with it. The PrintStream instance once closed won't work. Also a PrintStream instance once closed cannot be closed again. Syntax: public void close() 2 min read StringReader close() method in Java with Examples The close() method of StringReader Class in Java is used to close the stream and release the resources that were busy in the stream, if any. This method has following results: If the stream is open, it closes the stream releasing the resources If the stream is already closed, it will have no effect. 2 min read LongStream.Builder accept() method in Java LongStream.Builder accept(long t) is used to insert an element into the element in the building phase of stream. It accepts an element to the stream being built. Syntax: void accept(long t) Parameters: This method accepts a mandatory parameter t which is the element to input into the stream. Excepti 2 min read Reader close() method in Java with Examples The close() method of Reader Class in Java is used to close the stream and release the resources that were busy in the stream, if any. This method has following results: If the stream is open, it closes the stream releasing the resources If the stream is already closed, it will have no effect. If an 2 min read Stream takeWhile() method in Java with examples The takeWhile(java.util.function.Predicate) method returns a stream of the remaining elements of this stream after taken the longest prefix of elements that match the given predicate if the stream is ordered else a stream of a subset of elements taken from this stream that match the given predicate. 3 min read StringWriter close() method in Java with Examples The close() method oStringWriter Class in Java is used to close the writer. Closing a writer deallocates any value in it or any resources associated with it. The StringWriter instance once closed won't work. Also a StringWriter instance once closed cannot be closed again. Syntax: public void close() 2 min read BlockingDeque takeLast() method in Java with Examples The takeLast() method of BlockingDeque returns and removes the tail of the Deque container from it. The method throws an InterruptedException if it is interrupted while waiting. Syntax:  public E takeLast() Returns: This method returns the tail(last element) of the Deque container. Exception: The 2 min read ObjectInputStream close() method in Java with examples The close() method of the ObjectInputStream class in Java closes the input stream. Syntax: public void close() Parameters: This method does not accept any parameter. Return Value: This method does not returns anything. Below program illustrate the above method: Program 1: Java // Java program to ill 1 min read PushbackReader close() method in Java with Examples The close() method of PushbackReader Class in Java is used to close the stream and release the resources that were busy in the stream, if any. This method has following results: If the stream is open, it closes the stream releasing the resources If the stream is already closed, it will have no effec 2 min read Like