package io; //实现文件的复制,文件可以包括.jpg .avi .mp2 .ppt等等 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Copy { public static void main(String[] args) { long start=System.currentTimeMillis(); String src="C:\\Users\\ALHH\\Desktop\\a04.jpg"; String dest="C:\\Users\\ALHH\\Desktop\\a04.jpg"; copyFile(src,dest); long end=System.currentTimeMillis(); System.out.println(end-start); } public static void copyFile(String src,String dest){ File file1=new File(src); File file2=new File(dest); FileInputStream fis=null; try { fis = new FileInputStream(file1); } catch (FileNotFoundException e) { e.printStackTrace(); } FileOutputStream fos=null;if(fos!=null){ try { fos = new FileOutputStream(file2); } catch (FileNotFoundException e) { e.printStackTrace(); } byte[] b=new byte[1024]; int len; try { while((len=fis.read())!=-1){ fos.write(b, 0, len); } } catch (IOException e) { e.printStackTrace(); }finally{ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } }}
if(fis!=null){ try { fos.close();
} catch (IOException e) { e.printStackTrace(); } } }}
时间: 2024-10-09 21:42:34