首页 > 编程语言 > 详细

Java复制文件

时间:2015-01-15 18:08:47      阅读:249      评论:0      收藏:0      [点我收藏+]

public class FileUtil {
    private FileUtil() {
    }

  public static void copyFile(String srcFile,String targetPath){
        File src = new File(srcFile);
        if(!src.exists()){
            System.out.println("文件【"+src+"】不存在");
            return;
        }
        System.out.println("正在复制文件:"+src);
        int byteRead = 0;
        try {
            InputStream in = new FileInputStream(src);
            File tarPath = new File(targetPath);
            if(!tarPath.exists()){//如果目标路径不存在 , 则创建一个目标路径
                tarPath.mkdir();
            }
            File tar = new File(tarPath.getAbsolutePath(),src.getName());
            FileOutputStream out = new FileOutputStream(tar);
            byte[] b = new byte[1024];//缓存
            while((byteRead = in.read(b))!=-1){    
                out.write(b,0,byteRead);
            }
            in.close();
            out.close();
            System.out.println("文件复制完成!");
        } catch (Exception e) {
            System.out.println("文件复制失败!");
            e.printStackTrace();
        }
        
    }

}

Java复制文件

原文:http://www.cnblogs.com/nickhan/p/4226789.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!