Java如何使用FileChannel类复制文件?
下面的示例向您展示如何使用java.nio.channels.FileChannel该类复制文件。
package org.nhooo.example.io;
import java.io.*;
import java.nio.channels.FileChannel;
public class FileCopy {
public static void main(String[] args) {
//// 定义源文件和目标文件
File source = new File("D:/Temp/source.txt");
File target = new File("D:/Temp/target.txt");
// 创建源通道和目标通道
try (FileChannel sourceChannel = new FileInputStream(source).getChannel();
FileChannel targetChannel = new FileOutputStream(target).getChannel()) {
// 将数据从源通道复制到目标通道
targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
} catch (Exception e) {
e.printStackTrace();
}
}
}
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短