zoukankan      html  css  js  c++  java
  • java_26 缓冲流

    1.缓冲流:

       读取数据大量的文件时,读取的速度慢, 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() 写换行    
    
     *            linux   
    
     * */
    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();
        }
    }
    复制代码
  • 相关阅读:
    c 编译异常 switch 之a label can only be part of a statement and a declaration is not a statement
    释码大华虹膜识别
    嵌入式开发之davinci--- 8168 总的roi 编码
    cv resource
    图像增强之DDE---基于红外图像的数字图像细节增强DDE
    音频处理之去噪算法---基于pcm和g711的音频16000hz、8bit去噪声算法
    嵌入式开发之davinci--- 8148/8168/8127 中的alsa音频pcm g711 和aac 音频格式
    嵌入式开发之davinci--- 8148/8168/8127 中的音频alsa 采集
    嵌入式开发之davinci--- 8148/8168/8127 中swms、Mosaic’s、display 显示pal 模式
    目标跟踪之klt---光流跟踪法
  • 原文地址:https://www.cnblogs.com/smxbo/p/10717606.html
Copyright © 2011-2022 走看看