装饰流
【小城贝尔】
装饰皆为节点生,节点均是装饰心。
方法调用多相似,只因接口无多字。
缓冲字符字节流,好似蛟龙水中游。
操作效率升大半,字符新添行里看。
public class BufferdeDemo {
public static void main(String[] args) {
copyUseByte("E:/java1.mp4","E:/java1ChengCheng.mp4");
copyUseRedWri("src/InnerClass/InnerClass.java","src/IODemo/copy-innerclass.java");
}
//Bufferded字节流文件拷贝
public static void copyUseByte(String src , String desc){
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src));
out = new BufferedOutputStream(new FileOutputStream(desc));
byte[] bytes = new byte[1024];
int len = -1;
while ( (len = in.read(bytes)) != -1){
out.write(bytes,0,len);
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
//手动关闭流 但是jdk1.7以后使用try with resource 写法自动关闭流
closeIO(in,out);
}
}
//Buffered字符流文件拷贝
public static void copyUseRedWri(String src , String desc){
BufferedReader in = null;
BufferedWriter out = null;
try {
in = new BufferedReader(new FileReader(src));
out = new BufferedWriter(new FileWriter(desc));
String line = null;
while ((line = in.readLine()) != null){
out.write(line);
out.newLine();
}
out.flush();
}catch (Exception e){
e.printStackTrace();
}finally {
//手动关闭流 但是jdk1.7以后使用try with resource 写法自动关闭流
closeIO(in,out);
}
}
//因为所有的流都实现了closeable接口所以定义一个统一的方法关闭流
public static void closeIO(Closeable ...ios){
for (Closeable io:ios){
try {
if (io != null){
io.close();
}
System.out.println("io has closed ");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}