字节流FileStream和处理流(包装流)Buffered和字节流BufferedStream

1.FilelnputStream:文件输入流

在F盘创建一个txt文件,内容为hello world

使用FilelnputStream进行读取

    @Test
    public void readFile01() throws IOException {
        String path="f:\\hello.txt";
        int read=0;
        FileInputStream fs=null;
        try {
            //创建 FileInputStream 对象,用于读取文件
            fs = new FileInputStream(path);
            //从该输入流读取一个字节的数据。
            //如果返回-1 , 表示读取完毕
            while((read = fs.read())!=-1){
                System.out.print((char) read);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            fs.close();
        }
    }

使用byte[]进行读取,提升效率

    @Test
    public void readFile01() throws IOException {
        String path="f:\\hello.txt";
        int read=0;
        byte[] b=new byte[8];
        FileInputStream fs=null;
        try {
            //创建 FileInputStream 对象,用于读取文件
            fs = new FileInputStream(path);
            //将该输入流读取到的字节加入byte数组
            while((read = fs.read(b))!=-1){
                System.out.print(new String(b,0,b.length));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            fs.close();
        }
    }

2.FileOutputStream将内容写入文件,如果该文件不存在会自动创建一个

将hello world写入F盘hello.txt

    @Test
    public void writeFile() throws IOException {
        String path="f:\\hello.txt";
        FileOutputStream fo = null;
        try {
            //这种创建方法写入时会覆盖原来内容
            fo=new FileOutputStream(path);
            //fo=new FileOutputStream(path,true);追加到文件后面
            String str = "hello,world!";
            //str.getBytes() 可以把 字符串-> 字节数组
            fo.write(str.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            fo.close();
        }
    }

实现图片/音乐的拷贝

将F盘的01.png拷贝到F盘并命名为02.png

    @Test
    public void Copy() throws IOException {
        String path1="f:\\01.png";
        String path2="f:\\02.png";
        FileInputStream fs=null;
        FileOutputStream fo = null;
        byte[] b=new byte[1024];
        int read=0;
        try {
            fs=new FileInputStream(path1);
            fo=new FileOutputStream(path2);
            while ((read= fs.read(b))!=-1){
                //边读边写
                fo.write(b,0,b.length);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (fs!=null){
                fs.close();
            }
            if (fo!=null){
                fo.close();
            }
        }
    }

3. FileReader

 使用FileReader读取F盘的hello.txt

    @Test
    public void Read() throws IOException {
        String path="f:\\hello.txt";
        int read=0;
        FileReader fr=null;
        try {
            fr = new FileReader(path);
            while((read = fr.read())!=-1){
                System.out.print((char) read);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (fr!=null){
                fr.close();
            }
        }
    }

4..FileWriter

 总的来说:FilelnputStream和FileReader用法类似,FileOutputStream和FileWriter用法类似

1.节点流是底层流/低级流,直接跟数据源相接,
2.处理流(包装流)包装节点流,既可以消除不同节点流的实现差异,也可以提供更方
便的方法来完成输入输出。[源码理解]
3.处理流(也叫包装流)对节点流进行包装,使用了修饰器设计模式,不会直接与数据
源相连

优点:1.性能的提高:主要以增加缓冲的方式来提高输入输出的效率
2.操作的便捷:处理流可能提供了一系列便捷的方法来一次输入输出大批量的数据,使
用更加灵活方便

BufferedReader

    @Test
    public void buffer() throws Exception {
        String filepath ="e:\\hello.txt";
        BufferedReader br = new BufferedReader(new FileReader(filepath));
        String line;
        //按行读取,null表示读取完毕
        while ((line =br.readLine())!=null) {
            System.out.println(line);
        }
        //关闭包装流,它会自动关闭节点流
        br.close();
    }

BufferedWriter

    @Test
    public void buffer1() throws Exception {
        String filepath ="e:\\hello.txt";
        BufferedWriter bw = new BufferedWriter(new FileWriter(filepath,true));
        //插入一个换行
        bw.newLine();
        bw.write("123");
        bw.write("456");
        bw.write("789");
        //关闭包装流,它会自动关闭节点流
        bw.close();
    }

实现文件拷贝

1. BufferedReader 和 BufferedWriter 是按字符读取

2.不要去读取 二进制文件[声音,视频,doc, pdf ], 可能造成文件损坏

    @Test
    public void buffer2() throws Exception{
        String filepath ="f:\\Re.txt";
        String filepath1 ="e:\\Re.txt";
        BufferedReader br = new BufferedReader(new FileReader(filepath));
        BufferedWriter bw = new BufferedWriter(new FileWriter(filepath1));
        String line = null;
        while ((line = br.readLine()) != null) {
            bw.write(line);
        }
        br.close();
        bw.close();
    }

BufferedInputStream是字节流,在创建BufferedInputStream时,会创建一个内部缓冲区数组.

BufferedOutputStream是字节流,实现缓冲的输出流,可以将多个字节写入底层输出流中,而
不必对每次字节写入调用底层系统

使用他们,可以完成二进制文件拷贝

    @Test
    public void buffered() throws Exception{
        String filepath = "f:\\Re.png";
        String filepath1 = "e:\\Re.png";
        BufferedInputStream bi = new BufferedInputStream(new FileInputStream(filepath));
        BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(filepath1));
        byte[] buffer = new byte[1024];
        int len;
        while((len=bi.read(buffer))!=-1){
            bo.write(buffer, 0, len);
        }
        bi.close();
        bo.close();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1while(true){learn}

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值