缓冲流

醉酒当歌 提交于 2019-11-28 08:23:03

缓冲流

缓冲流:

   读取数据大量的文件时 ,读取的速度慢, java提供了一套缓冲流,提高IO流的效率。分为字节缓冲流和字符缓冲流。

★字节缓冲流:

    缓冲输出流:BufferedOutputStream  缓冲输入流:BufferesInputStream

BufferedOutputStream:

 

复制代码
/*
 * 字节输出流的缓冲流  作用  提高效率
 * 继承OutputStream
 * 构造方法 new BufferedOutputStream(OutputStream out);
 * 
 * */
public class BufferedOutputStreamDemo {
    public static void main(String[] args) throws Exception {
    //  1创建字节输出流   绑定文件
        //FileOutputStream fos =new FileOutputStream("c:\\buffer");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("c:\\buffer"));
     bos.write(65);//自动查码表  utf-8  0  1
     //输出字符串
     byte[] b= "hello world".getBytes();
     bos.write(b);
    //截取  
     bos.write(b, 3, 2);
     bos.close();
    }
}
复制代码

 

BufferesInputStream:

复制代码
/*
 *   字节输入流的缓冲流  
 *   继承InputStream
 *   构造方法
 * 
 * */
public class BufferedInputStreamDemo {
    public static void main(String[] args) throws Exception {
        //1  创建字节输入流的缓冲流对象    
        BufferedInputStream b = new BufferedInputStream(new 
                FileInputStream("c:\\buffer"));
        byte[] bs = new byte[1024];
        b.close();
        int len = 0;
        while((len=b.read(bs))!=-1){
            System.out.print(new String(bs,0,len));
        }
     }
}
复制代码

★字符缓冲流,用于文本数据的高速写入

  缓冲输入流BufferedReader  缓冲输出流BufferedWrite

BufferedReader

复制代码
public class BufferedReaderDemo {
    public static void main(String[] args) throws Exception {
        //创建字符输入缓冲流对象
        BufferedReader bu =new BufferedReader(new FileReader("c:\\bu.txt"));
        String len = null;
        while((len=bu.readLine())!=null){
            System.out.println(len);
        }
        bu.close();
    }
}
复制代码

BufferedWrite

复制代码
/*字符输出缓冲流
 * write();
 *  构造方法 BufferedWriter(Writer w);
 *  new line() 写换行    \r\n
 *            linux   \n
 * */
public class BufferedWriterDemo {
    public static void main(String[] args) throws IOException {
        //创建字符输出流  封装文件
        FileWriter fWriter = new FileWriter("c://bu.txt");
        BufferedWriter bufferedWriter = new BufferedWriter(fWriter);
        bufferedWriter.write("您好");
        bufferedWriter.newLine();//换行
        bufferedWriter.write(100);
        bufferedWriter.flush();
        bufferedWriter.write("床前明月光");
        bufferedWriter.close();
    }
}
复制代码

文件缓冲流的复制方式:

复制代码
/*
 *  文件的复制方式
 *  1 字节流读写单个字节
 *  2 字节流读写字节数组   2405ms
 *  3 字节流缓冲区 读写单个字节   4855ms
 *  4 字节流缓冲区读写字节数组  697ms
 * */
public class Copy {
    public static void main(String[] args) throws Exception {
        long timeStart = System.currentTimeMillis();
        copy4(new File("c:\\liu.mp4"), new File("d:\\liu.mp4"));
        long end = System.currentTimeMillis();
        System.out.println(end-timeStart);
    }
    // 4 字节流缓冲区读写字节数组  
    public static void copy4(File src,File desc) throws Exception{
        BufferedInputStream bu = new BufferedInputStream(new FileInputStream(src)); 
        BufferedOutputStream ou = new BufferedOutputStream(new FileOutputStream(desc)); 
      int len = 0;
      byte[] b=new byte[1024*10];
      while((len=bu.read(b))!=-1){
          ou.write(b,0,len);
          
      }
      ou.close();
      bu.close();
    }
    //3 字节流缓冲区 读写单个字节
    public static void copy3(File src,File desc) throws Exception{
        BufferedInputStream bu = new BufferedInputStream(new FileInputStream(src)); 
        BufferedOutputStream ou = new BufferedOutputStream(new FileOutputStream(desc)); 
      int len = 0;
      while((len=bu.read())!=-1){
          ou.write(len);
          
      }
      ou.close();
      bu.close();
    }
    //2 字节流读写字节数组
    public static void  copy2(File src,File desc) throws Exception{
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(desc);
        int len = 0;
        byte[] b =new byte[1024*10];
        while((len=fis.read(b))!=-1){
            fos.write(b,0,len);
        }
        fos.close();
        fis.close();
    }
        //1 字节流读写单个字节
    public static void  copy1(File src,File desc) throws Exception{
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(desc);
        int len = 0;
        while((len=fis.read())!=-1){
            fos.write(len);
        }
        fos.close();
        fis.close();
    }
}
复制代码
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!