黑马程序员技术交流社区
标题:
毕老师IO输入输出流的视频的问题
[打印本页]
作者:
Uncle.Lee
时间:
2013-11-17 12:29
标题:
毕老师IO输入输出流的视频的问题
使用模拟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();
}
}
复制代码
作者:
杨增坤
时间:
2013-11-17 13:27
希望对你有帮助,这是读取缓冲的原因,如果是你那样的话,那就是一直读取流中的开始,没有连续读取,那么当然歌曲的字节是乱的。我认为是这样的,,如果你不认为是这样的,那么认真思考一下!
无标题.png
(24.13 KB, 下载次数: 8)
下载附件
2013-11-17 13:26 上传
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2