码迷,mamicode.com
首页 > 其他好文 > 详细

实现文件的复制

时间:2016-05-20 19:29:35      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

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(); }
} }}

 

实现文件的复制

标签:

原文地址:http://www.cnblogs.com/alhh/p/5512809.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!