• 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();
        }
    }
    复制代码
  • 相关阅读:
    Oracle常见故障问题
    java前后端加密(转载)
    美团在Redis上踩过的一些坑-3.redis内存占用飙升(转载)
    tomcat访问(access)日志配置、记录Post请求参数(转)
    angular2学习资源汇总
    Ionic2 常见问题及解决方案
    URL Schemes(转载)
    (四)文本编辑器Vim/Vi
    C#接口作用的深入理解
    C#连接SQL Server数据库进行简单操作
  • 原文地址:https://www.cnblogs.com/smxbo/p/10717606.html
Copyright © 2020-2023  润新知