Java文件拷贝的几种实现方案

在jdk1.7之前,java中没有直接的类提供文件复制功能。下面就文件复制,提出几种方案。

jdk1.7中的文件复制

在jdk1.7版本中可以直接使用Files.copy(File srcFile, File destFile)方法即可。

private static void copyFileUsingJava7Files(File source, File dest)
throws IOException {
    Files.copy(source.toPath(), dest.toPath());
}

使用FileInputStream复制

/**
 *
 * @Title: copyFileUsingStream
 * @Description: 使用Stream拷贝文件
 * @param: @param source
 * @param: @param dest
 * @param: @throws IOException
 * @return: void
 * @throws
 */
public static void copyFileUsingStream(File source, File dest)
throws IOException {
    InputStream is = null;
    OutputStream os = null;
    try {
        is = new FileInputStream(source);
        os = new FileOutputStream(dest);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }
    } finally {
        if(is != null) {
            is.close();
        }
        if(os != null) {
            os.close();
        }
    }
}

使用FileChannel实现复制

/**
 * @throws IOException
 *
 * @Title: copyFileUsingChannel
 * @Description: 使用Channel拷贝文件
 * @param: @param source
 * @param: @param dest
 * @return: void
 * @throws
 */
public static void copyFileUsingChannel(File source, File dest) throws IOException {
    FileChannel sourceChannel = null;
    FileChannel destChannel = null;
    try {
        sourceChannel = new FileInputStream(source).getChannel();
        destChannel = new FileOutputStream(dest).getChannel();
        destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
    } finally {
        if(sourceChannel != null) {
            sourceChannel.close();
        }
        if(destChannel != null) {
            destChannel.close();
        }
    }
}

使用Apache Commons IO包中的FileUtils.copyFile复制

public static void copyFileUsingApacheIO(File source, File dest) throws IOException {
    FileUtils.copyFile(source, dest);
}

使用IO重定向实现复制

/**
 *
 * @Title: copyFileUsingRedirection
 * @Description: 使用Redirection拷贝文件
 * @param: @param source
 * @param: @param dest
 * @param: @throws IOException
 * @return: void
 * @throws
 */
public static void copyFileUsingRedirection(File source, File dest) throws IOException {
    FileInputStream in = null;
    PrintStream out = null;
    try {
        in = new FileInputStream(source);
        out = new PrintStream(dest);
        System.setIn(in);
        System.setOut(out);
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            System.out.println(sc.nextLine());
        }
    } finally{
        if(in != null) {
            in.close();
        }
        if(out != null) {
            out.close();
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,099评论 19 139
  • Java 语言支持的类型分为两类:基本类型和引用类型。整型(byte 1, short 2, int 4, lon...
    xiaogmail阅读 1,383评论 0 10
  • # 3.1 File # ## 3.1.1 File基本概念 ## 1.基本概念 -File类用于表示文件(目录)...
    闫子扬阅读 503评论 0 0
  • 本文包括:1、文件上传概述2、利用 Commons-fileupload 组件实现文件上传3、核心API——Dis...
    廖少少阅读 12,650评论 5 91
  • 爱情里最大的幸运是,你喜欢的那个人,恰好也喜欢你。 一直以来,你都是我眼里的风景。只不过,以前是美丽的风景,而现在...
    路思阅读 351评论 0 3