io中的AutoCloseable,Closeable和Flushable接口
2017-01-09 15:46
344 查看
AutoCloseable是在java.lang中的(1.7才有的)
Closeable(1.5才有的)接口继承了AutoCloseable接口
public void close()
关闭此流并释放与此流关联的所有系统资源。
继承Closeable接口,使代码更简洁,更清晰
当try代码快结束时,资源(在此时流)会被自动关闭
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class t {
private static BufferedReader reader;
public static void main(String[] args) {
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("a"))));
System.out.println(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class t implements Closeable {
private static BufferedReader reader;
public static void main(String[] args) {
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("a"))));
System.out.println(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void close() throws IOException {
reader.close();
}
}
flushable相同用法
Closeable(1.5才有的)接口继承了AutoCloseable接口
public void close()
关闭此流并释放与此流关联的所有系统资源。
继承Closeable接口,使代码更简洁,更清晰
当try代码快结束时,资源(在此时流)会被自动关闭
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class t {
private static BufferedReader reader;
public static void main(String[] args) {
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("a"))));
System.out.println(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class t implements Closeable {
private static BufferedReader reader;
public static void main(String[] args) {
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("a"))));
System.out.println(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void close() throws IOException {
reader.close();
}
}
flushable相同用法
相关文章推荐
- AutoCloseable, Closeable和Flushable接口
- JDK 1.7 java.io 源码学习之Closeable、Flushable、Appendable接口
- 探究java IO之AutoCloseable,Closeable和Flushable接口
- 探究java IO之AutoCloseable,Closeable和Flushable接口
- JDK 1.7 java.io 源码学习之AutoCloseable接口和try-with-resources语法
- Java 日看一类(28)之IO包中的Flushable接口和InputStream类
- java IO相关API探索之Closeable和InputStream接口
- JDK 1.7 java.io 源码学习之AutoCloseable接口和try-with-resources语法
- AutoCloseable,Closeable和Flushable
- java IO相关API探索之 AutoCloseable接口
- java 接口 java.io.Serializable
- 深入解析Java中Flushable接口的flush方法
- 安卓学习第十二天:接口,接口的使用,异常,异常处理,IO
- CXF webservice接口数据传输报错,ClientAbortException: java.io.IOException: 您的主机中的软件中止了一个已建立的连接
- IO端口和IO内存的区别及分别使用的函数接口
- linux基础——linux下多路IO复用接口之select/poll
- IO端口和IO内存的区别及分别使用的函数接口
- java.IO.Serializable 接口是什么功能?
- 基于Lustre文件系统的MPI-IO编程接口改进
- IO端口和IO内存的区别及分别使用的函数接口