字节缓冲流
- 缓冲流作用:提高操作,读写的性能。IO操作影响操作,缓冲区可以减少操作
- 内部自动释放节点流,只需要释放外部的流
- 手动释放是从里到外释放
- 默认是8K缓冲大小,也可以自己指定大小
BufferedInputStream
package com.io.cx3;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class buff {
public static void main(String[] args) {
File src = new File("abc.txt");
InputStream is =null;
try {
is =new BufferedInputStream(new FileInputStream(src));
byte[] flush = new byte[1024];
int len = -1;
while((len=is.read(flush))!=-1) {
String str = new String(flush,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(null!=is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void test1() {
File src = new File("dest.txt");
InputStream is =null;
BufferedInputStream bis =null;
try {
is =new FileInputStream(src);
bis = new BufferedInputStream(is);
byte[] flush = new byte[1024];
int len = -1;
while((len=is.read(flush))!=-1) {
String str = new String(flush,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(null!=is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(null!=bis) {
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
BufferedOutputStream
package com.io.cx3;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class buff1 {
public static void main(String[] args) {
File dest = new File("dest.txt");
OutputStream os =null;
try {
os =new BufferedOutputStream( new FileOutputStream(dest));
String msg ="这是新建文件";
byte[] datas =msg.getBytes();
os.write(datas,0,datas.length);
os.flush();
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
if (null != os) {
os.close();
}
} catch (Exception e) {
}
}
}
}