JAVA通过I/O流复制文件
本文是对字节流操作,可以多音频视频文件进行操作,亲测有效。
个人感觉这个东西就是靠记的, 没什么好解释的,,,,
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 7 8 public class CopyMove { 9 10 public static void main(String[] args){ 11 12 //时间 13 long starttime = 0; 14 long finishtime = 0; 15 Calendar calendar = null; 16 17 /* String oldPath = "D:/test/gktgd.mp4"; 18 String newPath = "D:/test/new4.mp4";*/ 19 String oldPath = "D:/test/fff.flv"; 20 String newPath = "D:/test/ff.flv"; 21 22 File oldFile = new File(oldPath); 23 File newFile = new File(newPath); 24 25 //若文件不存在则创建 26 if(!newFile.exists()){ 27 try { 28 newFile.createNewFile(); 29 } catch (IOException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } 33 } 34 35 FileInputStream fIS = null; 36 FileOutputStream fOS = null; 37 38 //声明用于传递字节的byte 数组大小要是2的整数倍 39 byte[] ttStream = new byte[4096]; 40 41 int len = 0; 42 43 try { 44 fIS = new FileInputStream(oldFile); 45 fOS = new FileOutputStream(newFile); 46 47 //获取开始时间 48 starttime = System.currentTimeMillis(); 49 50 System.out.println(starttime); 51 len = fIS.read(ttStream); 52 //若len为-1 证明读取完,若不为-1 则为读取的字节数 53 while((len)!=-1){ 54 //将读出的字节写入文件中 55 fOS.write(ttStream, 0, len); 56 //再次读取 57 len=fIS.read(ttStream); 58 } 59 //获得结束时间 60 finishtime = System.currentTimeMillis(); 61 System.out.println(finishtime); 62 System.out.println("时间间隔为:" +(finishtime-starttime)); 63 } catch (FileNotFoundException e) { 64 // TODO Auto-generated catch block 65 e.printStackTrace(); 66 } catch (IOException e) { 67 // TODO Auto-generated catch block 68 e.printStackTrace(); 69 } finally { 70 try { 71 //按照先使用后关闭的原则关闭 72 fOS.close(); 73 fIS.close(); 74 } catch (IOException e) { 75 // TODO Auto-generated catch block 76 e.printStackTrace(); 77 } 78 } 79 } 80 }