字节流复制文件
方法一: package cn.lideng.demo2; 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) throws FileNotFoundException { //建立两个交流对象 绑定数据源和目的地 FileInputStream fis=null; FileOutputStream fos=null; fis=new FileInputStream("d:\\b.txt"); fos=new FileOutputStream("c:\\b.txt"); int len=0; try { while((len=fis.read())!=-1){ fos.write(len); } } catch (IOException e) { System.out.println("文件复制失败"); } finally{ if(fos!=null){ try { fos.close(); } catch (Exception e2) { // TODO: handle exception } finally{ if(fis!=null){ try { fis.close(); } catch (Exception e3) { e3.printStackTrace(); System.out.println("释放资源失败"); } } } } } } } 方法二: package cn.lideng.demo2; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class CopyArr { public static void main(String[] args) { long c = System.currentTimeMillis(); FileInputStream fis=null; FileOutputStream fos=null; try { fis=new FileInputStream("d:\\b.txt"); fos=new FileOutputStream("c:\\b.txt"); byte[] b=new byte[1024]; int len=0; while((len=fis.read(b))!=-1){ fos.write(b,0,len); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(fos!=null){ try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(fis!=null){ try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException("释放资源失败"); } } } } long e=System.currentTimeMillis(); System.out.println(e-c); } } }