Java基础-IO流

1.File类,java.io包下。

2.io流

    // 文本等类型使用字符流,Reader输入流,Writer输出流
    // 视频,图片等使用字节类,InputStream输入流,OutputStream输出流
    /*
     * 字节流,处理图片视频等 FileInputStream
     */
    @Test
    public void test1(){
        File file1 = new File("2050550.jpg");
        File file2 = new File("复制的2050550.jpg");
        FileInputStream fileInputStream;
        FileOutputStream fileOutputStream;
        try {
            fileInputStream = new FileInputStream(file1);
            fileOutputStream = new FileOutputStream(file2);

        // 复制过程
            byte[] butter = new byte[10];
            int len;
            while ((len= fileInputStream.read(butter))!=-1){
                fileOutputStream.write(butter,0,len);
            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        try {
            if (fileInputStream != null)
                fileInputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            if (fileOutputStream != null)
                fileOutputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    /*
     *  缓冲字节流(处理流)BufferedInputStream
     */
    @Test
    public void test2(){
        BufferedInputStream bi = null;
        BufferedOutputStream bo = null;
        try {
            bi = new BufferedInputStream(new FileInputStream(new File("2050550.jpg")));
            bo = new BufferedOutputStream(new FileOutputStream(new File("缓冲字节流2050550.jpg")));

            byte[] buffer = new byte[1024];
            int len;
            while ((len = bi.read(buffer)) != -1){
                bo.write(buffer,0,len);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (bo != null) {
                try {
                    bo.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (bi != null){
                try {
                    bi.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /*
     * 缓冲字符流
     */
    @Test
    public void test3(){
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(new File("2050550.txt")));
            bw = new BufferedWriter(new FileWriter(new File("缓冲字符流2050550.txt")));

            char[] buffer = new char[1024];
            int len;
            while ((len = br.read(buffer)) != -1){
                bw.write(buffer,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

3.转换流。字节流和字符流之间的转换。

InputStreamReader:将InputStream转换为Reader

OutputStreamWriter:将Writer转换为OutputStream

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值