(JAVA)IO缓冲区

package IODemo;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @author Alina
 * @date 2021年10月18日 9:57 下午
 * 一、  1.IO流关于缓冲区,
 *      2.输出流缓冲区:BufferedOutputStream(OutputStream out)
 *      3.输入流缓冲区:BufferedInputStream(InputStream in)
 *      4.提高流对象传递效率
 *
 *
 */
public class FileIOStream {
    public static void main(String[] args) {
        long startime = System.currentTimeMillis();
        try {
            method_copy3();
        } catch (Exception e) {
            e.printStackTrace();
        }

        long endtime = System.currentTimeMillis();
        System.out.println(endtime - startime);

    }

    public static void method_bufferedout() throws Exception {
        //先创建OutputStream()对象,缓冲区方法依赖于流对象
        FileOutputStream fos = new FileOutputStream("/Users/yuzhang/Desktop/test.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bos.write("你好".getBytes(StandardCharsets.UTF_8));
        bos.close();
    }

    public static void method_bufferedin() throws Exception {
        FileInputStream fis = new FileInputStream("/Users/yuzhang/Desktop/test.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
//        int s = 0;
//        while ((s = bis.read()) != -1){
//            System.out.print((char)s);
//        }
//        bis.close();


        byte[] bytes = new byte[1024];
        int x = 0;
        while ((x = bis.read(bytes)) != -1) {
            System.out.print(new String(bytes, 0, x));
        }
        bis.close();
    }

    public static void method_copy1()  {
        //先创建两个流向,先赋值为空,以避免作用域不同导致无法使用
        FileInputStream fis = null;
        FileOutputStream fop = null;

        try {
            fis = new FileInputStream("/Users/yuzhang/Desktop/test.txt");
            fop = new FileOutputStream("src/IOdemo/test1.txt");
            //创建字节
            int bytes = 0;
            while ((bytes = fis.read()) != -1) {
                fop.write(bytes);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("复制失败");
        } finally {
            try {
                if (fop !=null) {
                    fop.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("复制失败");
            } finally {
                try{
                    if (fis != null) {
                        fis.close();
                    }
                }catch (Exception e ){
                    e.printStackTrace();
                    throw new RuntimeException("关闭数据源失败");
                }

            }
        }


    }

    public static void method_copy2(){
        //读写字节数组
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("/Users/yuzhang/Desktop/test.txt");
            fos = new FileOutputStream("src/IOdemo/test1.txt");
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fis.read(bytes))!= -1){
                fos.write( bytes,0,len);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("复制失败");
        }finally {
            try{
                if(fos !=null ){
                    fos.close();
                }
            }catch (Exception e){
                e.printStackTrace();
                throw new RuntimeException("关闭资源失败");
            }finally {
                try {
                    if (fis != null){
                        fis.close();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    throw new RuntimeException("关闭资源失败");
                }
            }

        }

    }
    public static void method_copy3(){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try{
            bis = new BufferedInputStream(new FileInputStream("/Users/yuzhang/Desktop/test.txt"));
            bos = new BufferedOutputStream(new FileOutputStream("src/IOdemo/test1.txt"));
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = bis.read(bytes)) != -1){
                bos.write(bytes,0,len);
            }
        }catch (Exception e ){
            e.printStackTrace();
            throw new RuntimeException(" 复制失败");
        }finally {
            try{
                if (bos != null){
                    bos.close();
                }
            }catch (Exception e ){
                e.printStackTrace();
                throw new RuntimeException("资源关闭异常");
            }finally {
                try{
                    if (bis != null){
                        bis.close();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    throw new RuntimeException("资源关闭异常");
                }
            }
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值