Java IO 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:

?
1 voidclose() throwsException

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:

?
1 voidflush() throwsIOException

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:

 Packagedemo;ImportJava.io.BufferedReader;ImportJava.io.FileInputStream;ImportJava.io.InputStreamReader;/*** Autocloseable interface, which represents a resource that needs to be closed when it is no longer in use. There is only one method under this interface, close (). This method is called automatically under try-with-* resource syntax, supports throwing exception, and of course it also encourages the throwing of more detailed exceptions. Close () does not recommend throwing thread interrupts * interruptedexception. For the implementation of this interface, the specification strongly recommends that close () be idempotent, that is to say, multiple calls to the close () method and the result of a call are the same. * JDK1.7 New features **/ Public classInputstreamreadertest { Public Static voidMain (string[] args) {Try(BufferedReader reader =NewBufferedReader (NewInputStreamReader (NewFileInputStream ("Src\\fuhd.txt"), "UTF8"), 1024) {System.out.println (Reader.readline ()); } Catch(Exception e) {e.printstacktrace (); }        //example, declare your own two resource classes to implement the Autocloseable interface.         Try(MyResource MyResource =NewMyResource (); MyResource2 MyResource2=NewMyResource2 ())            {Myresource.readresource ();        Myresource2.readresource (); } Catch(Exception e) {e.printstacktrace (); }    }}classMyResourceImplementsautocloseable {@Override Public voidClose ()throwsException {System.out.println ("Close Resource"); }     Public voidReadresource () {System.out.println ("Read Resource"); }}classMyResource2Implementsautocloseable {@Override Public voidClose ()throwsException {System.out.println ("Close Resource2"); }     Public voidReadresource () {System.out.println ("Read Resource2"); }}

Output

Heat............................ Read Resourceread resource2close resource2close Resource

Java IO 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.