import java.io.FileInputStream;
import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class Copyfile2 { public static void copyBytesFile(String sFilename,String dFilename) throws IOException{ //创建文件输入流对象和文件输出流对象 //文件输入流对象与源文件关联 FileInputStream fis = new FileInputStream(sFilename); //文件输出流对象与目标文件关联 //循环读取文件,同时写出到外部磁盘文件 FileOutputStream fos = new FileOutputStream(dFilename); //清空缓存、关闭流对象 byte[] b = new byte[1024];//从关联的源文件中读一个字节,如果到达文件末,返回值为-1 int read = fis.read(b); while(read !=-1){ fos.write(b);//向关联的目标文件写出一个字节 read = fis.read(b); } fis.close(); fos.close(); } //清空缓存,关闭流对象 /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub long t1,t2; t1 = System.currentTimeMillis(); try { Copyfile2.copyBytesFile("a.mp3","temp.mp3"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();} t2 = System.currentTimeMillis(); System.out.println("用时:" + (t2-t1)); }}