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 婆婆高寿祝福语大全简短