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();
}
} |