Explore Java Io's autocloseable,closeable and flushable interfaces

Source: Internet
Author: User

There are 3 interfaces that are important for a stream class. Two of these interfaces are closeable and flushable, which are defined in the java.io package and are added by JDK5 . The 3rd interface is autocolseable, which is a new interface added by JDK7 and packaged into a Java.lang package .

The Autocloseable interface provides support for JDK7 A newly added try statement with resources that can automate the resource shutdown process . Only objects with classes that implement the Autocloseable interface can be managed by a try statement with a resource. The Autocloseable interface defines only the close () method:

void Close () throws Exception

This method closes the calling object and frees all resources that may be occupied. The method is called automatically at the end of a try statement with a resource, eliminating the need to explicitly call the close () method.

The Closeable interface also defines the close () method. An object of a class that implements the Closeable interface can be closed. Starting with JDK7 ,closeable expanded the autocloseable. Therefore, in JDK7, all classes that implement the Closeable interface also implement the Autocloseable interface .

An object of a class that implements the flushable interface, which forces the output of the cache to be written to the stream associated with the object . The interface defines the flush () method, as follows:

void Flush () throws IOException

Flush flow often causes the output of the cache to be physically written to the underlying device. all I/O classes that write to the stream implement the Flushable interface .

Note: 3 key points about a try statement with resources :

    • The resource managed by a try statement with a resource must be an object of the class that implements the autocloseable interface.

    • The resource declared in the try code is implicitly declared as fianl.

    • You can manage multiple resources by separating each claim with a semicolon .

Also keep in mind that the scope of the declared resource is limited to a try statement with resources. The main advantage of a try statement with a resource is that when the try block ends, the resource (in this case, stream) is automatically closed. Therefore, it is unlikely that you will forget to close the stream. Making a try statement with a resource can often make the source code shorter, clearer, and easier to maintain. As an example:

package io;import java.io.bufferedreader;import java.io.file;import  java.io.fileinputstream;import java.io.ioexception;import java.io.inputstreamreader;public  Class inputstreamreadertest {    public static void main (String[]  args)  {        try (bufferedreader reader =  New bufferedreader (New inputstreamreader (                         new  FileInputStream (New file ("/home/fuhd/text")), "UTF8"), 1024x768) {             system.out.println (Reader.readline ());     //read a line here directly          }catch (ioexception e) {             e.printstacktrace ();         }    }} 


Explore Java Io's autocloseable,closeable and flushable interfaces

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.