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

    1       字节缓冲流

    字节缓冲流根据流的方向,共有2个

    l  写入数据到流中,字节缓冲输出流 BufferedOutputStream

    l  读取流中的数据,字节缓冲输入流 BufferedInputStream

    练习:用字节缓冲流复制文件

    public class Demo07 {
        public static void main(String[] args) throws IOException {
            long time1=System.currentTimeMillis();
            //明确数据源
            FileInputStream fis=new FileInputStream("D:\codetool\jdk1.8.zip");
            //添加缓冲区
            BufferedInputStream bis=new BufferedInputStream(fis);
            //添加目的地
            FileOutputStream fos=new FileOutputStream("D:\test\jdk1.8.zip");
            //添加缓冲区
            BufferedOutputStream bos=new BufferedOutputStream(fos);
            //开始复制
            byte[]bytes=new byte[1024];
            int len=0;
            while((len=bis.read(bytes))!=-1){
                bos.write(bytes,0,len);
            }
            //释放资源
            bis.close();
            bos.close();
            long time2=System.currentTimeMillis();
            System.out.println(time2-time1);
        }
    }

    2  字符缓冲流

    2.1    BufferedWriter

    void newLine() 根据当前的系统,写入一个换行符

    2.2  BufferedReader

    public String readLine() 读取一个文本行,包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null。

    练习:用字符缓冲流复制文本文件

    public class Demo08 {
        //newLine()
        //Windows:
    
        //Linux:
    
        //public String readLine()
        public static void main(String[] args) throws IOException {
            //m1();
            //m2();
            copy();
        }
        public static void m1() throws IOException{
            //明确目的地
            FileWriter fw=new FileWriter("D:\test\x.txt");
            //添加缓冲流
            BufferedWriter bw=new BufferedWriter(fw);
            bw.write("巴拉拉小魔仙");
            bw.newLine();
            bw.flush();
            bw.write("全身变");
            bw.newLine();
            //释放资源
            bw.close();
        }
        public static void m2() throws IOException{
            //明确数据源
            FileReader fr=new FileReader("D:\test\x.txt");
            //添加缓冲流
            BufferedReader br=new BufferedReader(fr);
            //开始读
            int linenum=0;
            String line=null;
            while((line=br.readLine())!=null){
                linenum++;
                System.out.println(linenum+" "+line);
            }
            //释放资源
            br.close();
        }
        public static void copy() throws IOException{
            //明确数据源
            FileReader fr=new FileReader("D:\test\x.txt");
            //添加缓冲流
            BufferedReader br=new BufferedReader(fr);
            //明确目的地
            FileWriter fw=new FileWriter("D:\test\e\x.txt");
            //添加缓冲流
            BufferedWriter bw=new BufferedWriter(fw);
            //开始复制
            String line=null;
            while((line=br.readLine())!=null){
                bw.write(line);
                bw.newLine();
                bw.flush();
            }
            //释放资源
            br.close();
            bw.close();
        }
    }
  • 相关阅读:
    Stl源码剖析读书笔记之Alloc细节
    Lua热更系统
    Linux C++线程池
    linux sort,uniq,cut,wc.
    (转)Linux grep
    用LogParser分析IIS请求压力
    (转)MySQL主从复制的常见拓扑、原理分析以及如何提高主从复制的效率总
    AIS相关资料
    python学习笔记
    (转)MySQL InnoDB修复笔记
  • 原文地址:https://www.cnblogs.com/quanjunkang/p/10655633.html
Copyright © 2011-2022 走看看