• 缓冲流


    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();
        }
    }
  • 相关阅读:
    01-文件系统
    适配器模式,新老系统兼容
    01-Entity FrameWork如何控制数据的变化
    .Net实战之反射操作篇
    .Net实战之反射相关类之间的人体经络关系
    .Net实战之反射外卖计费
    [转]UINavigationController 返回的方法汇总
    [转]AFNetWorking使用笔记
    vue----子组件引用vux popup mask遮罩在最上层解决办法 z-index问题
    vue系列---vue项目(已安装vuex)中引入jquery
  • 原文地址:https://www.cnblogs.com/quanjunkang/p/10655633.html
Copyright © 2020-2023  润新知