如何使用BufferedReader/Writer在Java中复制图像 [英] How to copy image in java using bufferedreader/writer

查看:147
本文介绍了如何使用BufferedReader/Writer在Java中复制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    File file = new File("download.png");
    File newfile = new File("D:\\Java.png");
    BufferedReader br=null;
    BufferedWriter bw=null;
    try {
        FileReader fr = new FileReader(file);
        FileWriter fw = new FileWriter(newfile);
        br = new BufferedReader(fr);
        bw = new BufferedWriter(fw);
        char[] buf = new char[1024];
        int bytesRead;
        while ((bytesRead = br.read(buf)) > 0) {
            bw.write(buf, 0, bytesRead);
        }
        bw.flush();
        }
        catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        }

此代码有什么问题. BufferedReader和Writer类可以实现吗? 我知道如何使用InputStream和OutputStream来复制图像,所以请不要使用它粘贴解决方案!

Whats wrong with this code. Is it possible with BufferedReader and Writer Class?? I know how to to make copy of image using InputStream and OutputStream, So don't paste solution using that!!

推荐答案

此代码有什么问题.

Whats wrong with this code.

您正在使用基于文本的类来存储二进制数据.

You're using text-based classes for binary data.

是否可以使用BufferedReader和Writer类?

Is it possible with BufferedReader and Writer Class?

不是,在处理二进制数据时,不是.

Not while you're dealing with binary data, no.

我知道如何使用InputStream和OutputStream来复制图像,所以请不要使用它粘贴解决方案!

I know how to to make copy of image using InputStream and OutputStream, So don't paste solution using that!

这是您应该使用的解决方案,因为这些是为二进制数据设计的类.

That's the solution you should use, because those are the classes designed for binary data.

从根本上讲,对于非文本数据使用ReaderWriter会损坏,并提出问题.如果您在文本编辑器中打开文件而看不到文本,则它不是文本文件...(或者,它可能是文本文件,您使用了错误的编码,但是图像和声音之类的东西不是自然的文字.)

Fundamentally, using Reader or Writer for non-text data is broken, and asking for trouble. If you open up the file in a text editor and don't see text, it's not a text file... (Alternatively, it could be a text file that you're using the wrong encoding for, but things like images and sound aren't naturally text.)

这篇关于如何使用BufferedReader/Writer在Java中复制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆