以前的流操作:
//1.声明
InputStream in=null;
OutputStream out=null;
//2.定义
in=System.in;
out=System.out;
//3.使用流
int date=-1;
try {
date=in.read();
out.write(date);
out.flush();//刷新缓冲区,强制所有数据写出去
} catch (IOException e) {
e.printStackTrace();
}
//4.关闭流,判断是否空指针
//要分别关闭in和out流,否则会出错
finally{
if(in!=null ) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out !=null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
**最新流操作:(将创建的流放入try的括号中,会自动关闭)
*直接使用流:
int date=-1;
try( InputStream in=System.in; OutputStream out=System.out;) {
date=in.read();
out.write(date);
out.flush();//刷新缓冲区,强制所有数据写出去
} catch (IOException e) {
e.printStackTrace();
}