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字
12 学生节祝福语大全简短
13 新疆新年祝福语大全简短
14 过节文案成语祝福语简短
15 过年爱情祝福语大全简短
16 早晨好正能量祝福语简短
17 高考前祝福语简短字句
18 女儿生日祝福语说说简短