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 婚前女儿生日祝福语简短