黑马程序员技术交流社区

标题: 转换流。 [打印本页]

作者: 王德升    时间: 2012-7-7 17:13
标题: 转换流。
一:

class MyBufferedText
{  
        public static void main(String[] args)throws IOException
        {
                MyBufferedInputStream bufis = new MyBufferedInputStream(new FileInputStream("C:\\haoting.mp3"));  

                BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("C:\\ting.mp3"));  

                       int ch = 0;  

                       while((ch=bufis.myRead())!=-1)
                {  
                           bufos.write(ch);  //一直不明白这是怎么写入的,到底是通过什么方式写入的?
                }
  
                        bufis.myClose();  

                bufos.myClose();  
        }
}  


class MyBufferedInputStream
{  
        private InputStream in;  

        private byte[] buf = new byte[1024];  

        private int pos =0,count = 0;

        MyBufferedInputStream(InputStream in)
        {  
            this.in = in;  
        }  
  
        public int myRead()
        {  

            if(count==0)
            {  
                count = in.read(buf);  

                if(count<0) //为什么要做这步判断呢?

                    return -1;

                pos = 0;

                byte b = buf[pos];

                count--;  

                pos++;  

                return b&255; //这里与上255是什么意思啊,希望说的具体点,听老毕说的一头雾水啊,!!
            }

            else if(count>0)
            {  
                byte b = buf[pos];

                count--;  

                pos++;  

                return b&255;  
            }  
        }
         
    public void myClose()throws IOException
    {  
        in.close();  
    }  
}  

二:
麻烦跟我细细说下字节流转换成字符流: InputStreamReader

还有字符转换成字符流转换成字节流:OutputStreamWriter

这个我知道,就是不知道怎么去用,怎么去区分??老师说的好多绕来绕去我都昏了,

麻烦大仙给我说说是怎么回事,谢谢了,





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2