本帖最后由 韩俊杰 于 2013-1-14 12:47 编辑
- package io;
- import java.io.*;
- 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()throws IOException{
- //通过in对象读取硬盘上的数据,并存储到buf中
- if(count==0){
- count=in.read(buf);
- if(count<0)
- return -1;
- pos=0;
- byte b=buf[pos];
- count--;
- pos++;
- return b&255;
- }else if(count>0){
- byte b=buf[pos];
- count--;
- pos++;
- return b&0xff;
- }
- return -1;
- }
- public void myClose()throws IOException{
- in.close();
- }
- }
- public class CopyMp3Demo {
- public static void main(String[] args)throws IOException {
- long start=System.currentTimeMillis();
- copy();
- long end=System.currentTimeMillis();
- System.out.println((end-start)+"毫秒");
- }
- //通过字节流缓冲区进行复制
- public static void copy()throws IOException{
- MyBufferedInputStream bis=new MyBufferedInputStream(new FileInputStream("1.mp3"));
- BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("2.mp3"));
- int ch=0;
- while((ch=bis.myRead())!=-1){
- bos.write(ch);
- }
- bis.myClose();
- bos.close();
- }
- }
复制代码 b&255,为什么b&255拷贝图片就成功了?请各位大神指点
|