集册 Java实例教程 使用FileChannel.transferTo()复制文件

使用FileChannel.transferTo()复制文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

575
FileChannel.transferTo()将字节从一个通道的文件传输到给定的可写字节通道。

import java.io.IOException;

import java.nio.channels.FileChannel;

import java.nio.file.Path;//时 代 J a v a - N o w J a v a . c o m

import java.nio.file.Paths;

import java.nio.file.StandardOpenOption;

import java.util.EnumSet;


public class Main {

  public static void main(String[] args) {

    Path copy_from = Paths

        .get("C:/folder1/folder2/folder3/videos/test.mp4");

    Path copy_to = Paths.get("C:/test.mp4");

    int bufferSizeKB = 4;


    System.out.println("Using FileChannel.transferTo method ...");//时   代     Java  公  众  号 - nowjava.com

    try (FileChannel fileChannel_from = (FileChannel.open(copy_from,

        EnumSet.of(StandardOpenOption.READ)));

        FileChannel fileChannel_to = (FileChannel

            .open(copy_to, EnumSet.of(StandardOpenOption.CREATE_NEW,

    
展开阅读全文