使用模拟BufferedInputStream类的功能,自定义的MyBufferedInputStream类复制MP3
发现复制成功了,复制出来的MP3和原文件大小相同,但是就是播放不了。。
求大神给看下那儿出错了。。- //通过缓冲流复制MP3
- package day19;
- import java.io.BufferedOutputStream;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class MP3CopyDemo {
- public static void main(String args[]) {
- MyBufferedInputStream bufis = null;
- BufferedOutputStream bufos = null;
- try {
- bufis = new MyBufferedInputStream(new FileInputStream(
- "D:\\KuGou\\张杰 - 我们都一样.mp3"));
- bufos = new BufferedOutputStream(new FileOutputStream(
- "D:\\张杰 - 我们都一样.mp3"));
- int bt = 0;
- while ((bt = bufis.read()) != -1) {
- bufos.write(bt);
- }
- } catch (IOException e) {
- // throw new RuntimeException("复制文件失败");
- e.printStackTrace();
- } finally {
- try {
- if (bufis != null)
- bufis.close();
- } catch (IOException e) {
- throw new RuntimeException("关闭输入流失败");
- } finally {
- try {
- if (bufos != null)
- bufos.close();
- } catch (IOException e) {
- throw new RuntimeException("关闭输出流失败");
- }
- }
- }
- }
- }
复制代码- //模拟BufferedInputStream
- package day19;
- import java.io.IOException;
- import java.io.InputStream;
- public class MyBufferedInputStream {
- private InputStream is=null;
- private int count=0,pos=0;
- MyBufferedInputStream(InputStream is){
- this.is=is;
- }
- public int read() throws IOException{
- byte bt[]=new byte[1024];
- if(count==0){
- count = is.read(bt);
- if(count<0)
- return -1;
- pos=0;
- byte b=bt[pos];
- pos++;
- count--;
- return b&0xff;
- }else if(count!=0){
- byte b=bt[pos];
- pos++;
- count--;
- return b&0xff;
- }
- return -1;
- }
- public void close() throws IOException{
- is.close();
- }
- }
复制代码 |