缓冲概述
在读写的操作中,读写操作的效率是十分重要的,sun公司提供了一套相应的类来提升效率。
==IO对象==没有具体的操作,它是找操作系统来实现这个操作,所以每个字节都去找操作系统,就会很慢,buffer就是先写入缓冲区,等写到一定的数量在一次性找操作系统写入文件。
BufferedOutputStream
该类实现了缓冲的输出流,通过设置这种输出流,应用程序就可以将各个字节写入底层的输出流中,而不必针对每个字节调底层输出流
。
|--OutputStream
|--BufferedOutputStream
public BufferedOutputStream(OutputStream out)
public BufferedOutputStream(OutputStream out, int size)
public void write(byte[] b, int off, int len)
public void write(int b)将指定的字节写入此缓冲的输出流
public void flush()
FileOutputStream fos = new FileOutputStream("xxxx");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("xxx");
byte[] bytes = "xxxx".getBytes();
bos.write(byte);
bos.close();
BufferedInputStream
我们一直使用流对象就BufferedInputStream,打印流printStream
|--InputStream
|--BufferedInputStream
public BufferedInputStream(InputStream in)
public BufferedInputStream(InputStream in, int size)
public void read(byte[] b);
public void flush();
FileInputStream
四种复制方法的效率比较
字节流读取单个字节
FileInputStream fis = null;
FileOutputStream fos = null;
int len = 0;
try{
fis = new FileInputStream("xxx");
fos = new FileOutputStream("xxx");
while((len = fis.read()) != -1){
fos.write((char)len);
}
}catch(IOException ex){
System.out.println(ex.getMessage("读写失败"));
}finally{
if((fis != null) || (fos != null)){
try{
fis.close()
fos.close()
}catch(IOException ex){
System.out.println(ex.getMessage("关闭流失败"));
}
}
}
字节流读取字符数组
FileInputStream fis = null;
FileOutputStream fos = null;
byte[] bytes = new byte[1024];
int len = 0;
try{
fis = new FileInputStream("xxx");
fos = new FileOutputStream("xxx");
while((len = fis.read(bytes)) != -1){
fos.write(bytes, 0 ,len);
}
}catch(IOException ex){
System.out.println(ex.getMessage("读写失败"));
}finally{
if((fis != null) || (fos != null)){
try{
fis.close()
fos.close()
}catch(IOException ex){
System.out.println(ex.getMessage("关闭流失败"));
}
}
}
字节流缓冲区读取单个字节
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
int len = 0;
try{
fis = new FileInputStream("xxx");
fos = new FileOutputStream("xxx");
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
while((len = bis.read()) != -1){
bos.write((char)len);
}
}catch(IOException ex){
System.out.println(ex.getMessage("读写失败"));
}finally{
if((bis != null) || (bos != null)){
try{
bis.close()
bos.close()
}catch(IOException ex){
System.out.println(ex.getMessage("关闭流失败"));
}
}
}
字节流缓冲区读取字符数组
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
int len = 0;
byte[] bytes = new byte[1024];
try{
fis = new FileInputStream("xxx");
fos = new FileOutputStream("xxx");
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
while((len = bis.read(bytes)) != -1){
bos.write(bytes, 0 ,len);
}
}catch(IOException ex){
System.out.println(ex.getMessage("读写失败"));
}finally{
if((bis != null) || (bos != null)){
try{
bis.close()
bos.close()
}catch(IOException ex){
System.out.println(ex.getMessage("关闭流失败"));
}
}
}
BufferedWriter
|--Writer
|--OutputStreamWriter
|--BufferedWriter
public BufferedWriter(Writer writer)
public void newLine()
BufferedReader
|--Reader
|--InputStreamReader
|--BufferedReader
public BufferedReader(Reader reader)
public String readLine()
IO流对象的使用步骤
- 明确操作的是数据源还有数据目的
- 源,InputStream ,Reader
- 目的, OutputStrea, Writer
- 要操作的是文本还是字节
- 明确数据所在的设备
- 硬盘,文件File对象
- 内存,数组,String
- 键盘,System.in,System.out
- 网络,Socker
- 是否需要额外的功能
- 转换吗?转换流InputStreamReader,OutputStreamWriter
- 高效吗?BufferedInputStream,BufferedOutputStream,BufferedReader,BufferedWriter