A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© zcbyzcb 中级黑马   /  2013-8-21 23:25  /  2103 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 forward 于 2013-8-24 21:18 编辑

字节流能否读取字符流数据呢,如操作图片,视频呀,又是为什么呢

7 个回复

倒序浏览
能上传图片啊  比老师的视频中有讲到。。
回复 使用道具 举报
字节流当然能读图片了
回复 使用道具 举报
除纯文本文件外,其他不都是用字节流读取么?
回复 使用道具 举报
其实好像没有字节流读取字符流的数据这种用法。如果要读取字符数据的话,可以用getBytes()将字符串转换成字节数组,然后用字节流读取就行了。字节流可以处理设备上的所有数据,包括字符数据。不过用字节流读取字符流数据没什么意义,就算有效率也低。字符流才能读取字节流中的数据,要用到转换流InputStreamReader。图片和视频是存的是二进制数据,非纯文本,用字节流来读取,也可以用字符流来读取,不过要用转换流将字节流转成字符流,如果要提高效率就用BufferedStream
BufferedReader bufr = new BufferedReader(new InputStreamReader(InputStream);
希望对你有帮助。。。
回复 使用道具 举报
应该不能直接读图片,可以用二进制文件读取图片,其实就是用字节流读取图片。其实就是用Java中提供的对字节流操作的类去操控图片文件

import java.io.*;
import javax.swing.*;
public class Start
{
        public static void main(String args[]) throws Exception{
               
                //源文件,必须存在,路径可选
                File sf = new File("H:/javapro/files/source.jpg");       
               
                //目的文件,因为要向其中写入,指定文件可以不存在,由程序生成
                File df = new File("H:/javapro/files/dest.jpg");
                new ReadWriteGra(sf,df);
                new UseGra(df);
        }
}

class ReadWriteGra
{
        FileInputStream in = null;
        FileOutputStream out = null;
        public ReadWriteGra(File sourceFile,File destFile) throws Exception{
                byte[] buf = new byte[1024];
                int len = 0;
                in = new FileInputStream(sourceFile);
                out = new FileOutputStream(destFile,true);
                while( (len = in.read(buf)) != -1 ){
                        out.write(buf,0,len);
                }
                out.close();
        }
}
class UseGra extends JFrame
{
        public UseGra(File picFile) throws Exception{

                this.setVisible(true);
                this.setResizable(false);
                this.setLayout(null);
                this.setBounds(600, 200, 400, 370);
                this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                JPanel p1 = (JPanel)this.getContentPane();
                p1.setOpaque(false);
                p1.setLayout(null);
                InputStream is = new FileInputStream(picFile);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int b = 0;
                while((b = is.read())!=-1){
                        baos.write(b);
                }
                ImageIcon image = new ImageIcon(baos.toByteArray());
                JLabel background = new JLabel(image);
                this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
                background.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());
                JButton bt = new JButton("Test_Button");
                p1.add(bt);
                bt.setBounds(10,10,150,25);
                validate();
        }
}
回复 使用道具 举报
说反了,I'm sorry
回复 使用道具 举报
黑马-文鸿利 发表于 2013-8-22 07:45
应该不能直接读图片,可以用二进制文件读取图片,其实就是用字节流读取图片。其实就是用Java中提供的对字节 ...

说反了,我想说的是字符流能读取字节流数据吗,如图片{:soso_e136:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马