1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileOutputStream; 4 import java.io.InputStream; 5 6 7 public class FileCopy { 8 9 public static void copy(File a,File b){ 10 try 11 { 12 int bytesum = 0; 13 int byteread = 0; 14 File oldfile = new File( "E:/java/copyfile/a.mp3"); 15 if ( oldfile.exists() ) 16 { //文件存在时 17 InputStream inStream = new FileInputStream( "E:/java/copyfile/a.mp3" ); //读入原文件 18 FileOutputStream fs = new FileOutputStream( "E:/java/copyfile/b.mp3" ); 19 byte[] buffer = new byte[ 1444 ]; 20 int length; 21 while ( ( byteread = inStream.read( buffer ) ) != -1 ) 22 { 23 bytesum += byteread; //字节数 文件大小 24 25 fs.write( buffer, 0, byteread ); 26 } 27 inStream.close(); 28 } 29 } 30 catch ( Exception e ) 31 { 32 System.out.println( "复制单个文件操作出错" ); 33 e.printStackTrace();} 34 35 } 36 public static void main(String[] args) 37 { 38 39 40 41 File a = new File(""); 42 File b = new File(""); 43 long start,end; 44 start = System.currentTimeMillis(); 45 copy(a,b); 46 end = System.currentTimeMillis(); 47 System.out.println("复制所用时间:" + (end - start) + "ms"); 48 } 49 50 51 52 }
![](https://img2024.cnblogs.com/blog/35695/202407/35695-20240713070336838-1837943664.jpg)