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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© cehkongfu 中级黑马   /  2012-11-28 23:58  /  1518 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package cn.heima.stream;
/*
演示mp3的复制。通过缓冲区。
BufferedOutputStream
BufferedInputStream
*/
import java.io.*;
class  CopyMp3
{
public static void main(String[] args) throws IOException
{
  long start = System.currentTimeMillis();
  copy_2();
  long end = System.currentTimeMillis();
  System.out.println((end-start)+"毫秒");
}
public static void copy_2()throws IOException
{
  MyBufferedInputStream bufis = new MyBufferedInputStream(new FileInputStream("0.mp3"));
  BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("2.mp3"));
  
  int by = 0;
  System.out.println("第一个字节:"+bufis.myRead());
  while((by=bufis.myRead())!=-1)
  {
   bufos.write(by);
  }
  bufos.close();
  bufis.myClose();
}
//通过字节流的缓冲区完成复制。
public static void copy_1()throws IOException
{
  BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("0.mp3"));
  BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("1.mp3"));
  
  int by = 0;
  while((by=bufis.read())!=-1)
  {
   bufos.write(by);
  }
  bufos.close();
  bufis.close();
}
}

class MyBufferedInputStream
{
private InputStream in;
private byte[] buf = new byte[1024*4];
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++;
   System.out.println("b:"+b);
   System.out.println("b&0xff:"+ (b&0xff) );
   return b&0xff;
  }
  return -1;
}

public void myClose()throws IOException
{
  in.close();
}
}


提问:bufos.write(by);  其中的by是经过提升的,比如byte型的负数经过 &0xFF 运算后会提升为整形的正数,也就是返回的 int的 by,本来时byte型的负数,提升为int的by,再将by写入,为什么没有导致乱码呢??



评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

3 个回复

倒序浏览
int by = 0;
  while((by=bufis.read())!=-1)  //在这里,read方法把byte的类型的提升为int型的  {
   bufos.write(by);  //但write的方法只会读取最低8位的有效部分  }
  bufos.close();
  bufis.close();
}
}

class MyBufferedInputStream
{
private InputStream in;
private byte[] buf = new byte[1024*4];
private int pos = 0,count = 0;

MyBufferedInputStream(InputStream in)
{
  this.in = in;
}
public int myRead()throws IOException
{
  
  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++;
   System.out.println("b:"+b);
   System.out.println("b&0xff:"+ (b&0xff) );
   return b&0xff;
  }
  return -1;
}

public void myClose()throws IOException
{
  in.close();
}
}

基础视频老毕有说过,你可以再重复看一遍!!

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
import java.io.*;
class  CopyMp3
{
public static void main(String[] args) throws IOException
{
  long start = System.currentTimeMillis();
  copy_2();
  long end = System.currentTimeMillis();
  System.out.println((end-start)+"毫秒");
}
public static void copy_2()throws IOException
{
  MyBufferedInputStream bufis = new MyBufferedInputStream(new FileInputStream("0.mp3"));
  BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("2.mp3"));
  
  int by = 0;
  System.out.println("第一个字节:"+bufis.myRead());
  while((by=bufis.myRead())!=-1)
  {
   bufos.write(by);
  }
  bufos.close();
  bufis.myClose();
}
//通过字节流的缓冲区完成复制。
public static void copy_1()throws IOException
{
  BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("0.mp3"));
  BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("1.mp3"));
  
  int by = 0;
  while((by=bufis.read())!=-1)
  {
   bufos.write(by);
  }
  bufos.close();
  bufis.close();
}
}

class MyBufferedInputStream
{
private InputStream in;
private byte[] buf = new byte[1024*4];
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++;
   System.out.println("b:"+b);
   System.out.println("b&0xff:"+ (b&0xff) );
   return b&0xff;
  }
  return -1;
}

public void myClose()throws IOException
{
  in.close();
}
}

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
黑马_郑亮新 发表于 2012-11-29 00:58
int by = 0;
  while((by=bufis.read())!=-1)  //在这里,read方法把byte的类型的提升为int型的  {
   buf ...

谢谢,在下明白了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马