package IOByteStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author 21959
*
*/
public class CopyMp3Demo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//copy_1();
//copy_2();
copy_3();
//copy_4();
}
//复制MP3文件,方式一,读取效率低
public static void copy_1() throws IOException {
FileInputStream fis1 = new FileInputStream("E:\\software\\QQMusic\\Download\\萧敬腾\\会痛的石头.mp3");
FileOutputStream fos1 = new FileOutputStream("会痛的石头1.mp3");
int ch1 = 0;
while((ch1=fis1.read())!=-1)
fos1.write(ch1);
fis1.close();
fos1.close();
}
//复制MP3文件,方式二,使用字节数组
public static void copy_2() throws IOException {
FileInputStream fis2 = new FileInputStream("E:\\software\\QQMusic\\Download\\萧敬腾\\会痛的石头.mp3");
FileOutputStream fos2 = new FileOutputStream("会痛的石头2.mp3");
byte[] ch2 = new byte[1024];
int len2=0;
while((len2=fis2.read(ch2))!=-1)
fos2.write(ch2,0,len2);
fis2.close();
fos2.close();
}
//复制MP3文件,方式三,建立字节缓冲区
public static void copy_3() throws IOException {
FileInputStream fis3 = new FileInputStream("E:\\software\\QQMusic\\Download\\萧敬腾\\会痛的石头.mp3");
FileOutputStream fos3 = new FileOutputStream("会痛的石头3.mp3");
BufferedInputStream bis = new BufferedInputStream(fis3);
BufferedOutputStream bos = new BufferedOutputStream(fos3);
int ch3 = 0;
while((ch3=bis.read())!=-1) {
bos.write((char)ch3);
bos.flush();
}
bis.close();
bos.close();
}
//复制MP3文件,方式四,使用available,不建议
public static void copy_4() throws IOException {
FileInputStream fis4 = new FileInputStream("E:\\software\\QQMusic\\Download\\萧敬腾\\会痛的石头.mp3");
FileOutputStream fos4 = new FileOutputStream("会痛的石头4.mp3");
byte[] ch4 = new byte[fis4.available()];
fis4.read(ch4);
fos4.write(ch4);
//fos4.flush();
fis4.close();
fos4.close();
}
}
Java笔记二十一 字节流练习——复制MP3文件的四种方式
最新推荐文章于 2021-12-14 19:54:58 发布