黑马程序员技术交流社区
标题:
IO流的一些疑问
[打印本页]
作者:
终结者
时间:
2013-11-1 16:09
标题:
IO流的一些疑问
import java.io.*;
class CopyMp3Test
{
public static void main(String[] args) throws IOException
{
long start=System.currentTimeMillis();
//copy();
//copy1();
copy3();
//copy2();
long end=System.currentTimeMillis();
System.out.println(end-start+"毫秒");
}
public static void copy() throws IOException{ //为什么用下面方法虽然能播放但是在播放时歌曲名字却出了问题(字节没变少),播放的歌曲时间显示不了
FileInputStream in=new FileInputStream("3.mp3");
FileOutputStream out=new FileOutputStream("5.mp3");
byte[] buf=new byte[1024];
int len=0;
while((len=in.read(buf))!=-1){
out.write(buf,0,len);
}
in.close();
out.close();
}
public static void copy3() throws IOException{ //为什么用下面方法复制不了歌曲
FileInputStream in=new FileInputStream("3.mp3");
FileOutputStream out=new FileOutputStream("9.mp3");
int len=0;
while((len=in.read())!=-1){
out.write(len);
}
in.close();
out.close();
}
public static void copy1() throws IOException{ //虽然复制速度快了,但是为什么在没有关闭资源的情况下,歌曲的字节变少了却能播放而上面的方法在没有关闭资源的情况下字节却没变少
BufferedInputStream in=new BufferedInputStream(new FileInputStream("3.mp3"));
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("6.mp3"));
byte[] buf=new byte[1024];
int len=0;
while((len=in.read(buf))!=-1){
out.write(buf,0,len);
}
in.close();
out.close();
}
public static void copy2() throws IOException{
BufferedInputStream in=new BufferedInputStream(new FileInputStream("3.mp3"));
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("7.mp3"));
int len=0;
while((len=in.read())!=-1){
out.write(len);
}
in.close();
out.close();
}
}
作者:
qixing0918
时间:
2013-11-2 10:17
public class CopyMusic {
public static void main(String[] args) throws Exception {
// copy();
// copy2();
copy3();
}
public static void copy() throws Exception {
// 我复制的是一个1.5m的MP3的歌 这个方法太慢了 得30-60秒
FileInputStream fis = new FileInputStream(new File("d:\\star.mp3"));
FileOutputStream fos = new FileOutputStream(new File("d:\\copy.mp3"));
int len = 0;
while ((len = fis.read()) != -1) {
fos.write(len);
}
fos.close();
fis.close();
}
public static void copy2() throws Exception {
// 这个方法还是非常快的 3秒
FileInputStream fis = new FileInputStream(new File("d:\\star.mp3"));
FileOutputStream fos = new FileOutputStream(new File("d:\\copy2.mp3"));
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
fis.close();
}
public static void copy3() throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("d:\\star.mp3")));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("d:\\copy3.mp3")));
int len = 0;
while ((len = bis.read()) != -1) {
bos.write(len);
}
bos.close();
bis.close();
}
}
复制代码
我的3个copy都正确 就是第一个太慢了
而且字节都不少 都能正常播放 !!!!!!!!!!!
作者:
终结者
时间:
2013-11-2 22:50
你还是没解决我上面的疑问,我问的是为什么,不是怎么做,我要的答案是原理
作者:
狼王
时间:
2013-11-3 07:32
加油哈,好好努力,为了黑马
作者:
明月几时有
时间:
2013-11-17 01:01
我来回答你的第三个问题,close的作用是关闭此文件输出流并释放与此流有关的所有系统资源。也就是你还有一部分资源在流中没刷出来!必须close
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2