如何使用Java NIO通道传输复制文件?
作者:WeekendFlower
时间:2023-04-27
来源:互联网
浏览:0
通过JAVANIO通道传输拷贝文件方式一/***通过JAVANIO通道传输拷贝文件**@paramsourcePath源文件路径*@paramtargetPath目标文件路径*/publicstaticvoidcopyFileByChannelTransfer(StringsourcePath,StringtargetPath){FileChannelinChannel=null;FileChanneloutChannel=null;try{//获取通道inChannel=FileChannel.open
通过JAVA NIO 通道传输拷贝文件
方式一
/**
* 通过JAVA NIO 通道传输拷贝文件
*
* @param sourcePath 源文件路径
* @param targetPath 目标文件路径
*/
public static void copyFileByChannelTransfer(String sourcePath, String targetPath) {
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
//获取通道
inChannel = FileChannel.open(Paths.get(sourcePath), StandardOpenOption.READ);
outChannel = FileChannel.open(Paths.get(targetPath),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE);
inChannel.transferTo(0,inChannel.size(),outChannel);
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭流
try {
if (outChannel != null) {
outChannel.close();
}
if (inChannel != null) {
inChannel.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}方式二
/**
* 通过JAVA NIO 通道传输拷贝文件
*
* @param sourcePath 源文件路径
* @param targetPath 目标文件路径
*/
public static void copyFileByChannelTransfer2(String sourcePath, String targetPath) {
FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
fis = new FileInputStream(sourcePath);
fos = new FileOutputStream(targetPath);
//获取通道
inChannel = fis.getChannel();
outChannel = fos.getChannel();
inChannel.transferTo(0,inChannel.size(),outChannel);
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭流
try {
if (outChannel != null) {
outChannel.close();
}
if (inChannel != null) {
inChannel.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}使用示例
String source = "e:\\demo\\纵天神帝.txt";
String target = "e:\\demo\\";
long time1 = System.currentTimeMillis();
copyFileByStream(source, target + "1.txt");
System.out.println("通过字节流实现文件的拷贝耗时:" + (System.currentTimeMillis() - time1));
long time2 = System.currentTimeMillis();
copyFileByReaderAndWriter(source, target + "2.txt");
System.out.println("通过字符流实现文件的拷贝耗时:" + (System.currentTimeMillis() - time2));
long time3 = System.currentTimeMillis();
copyFileByBuffered(source, target + "3.txt");
System.out.println("通过字节缓冲流实现文件的拷贝耗时:" + (System.currentTimeMillis() - time3));
long time4 = System.currentTimeMillis();
copyFileByBufferedChar(source, target + "4.txt");
System.out.println("通过字符缓冲流实现文件的拷贝耗时:" + (System.currentTimeMillis() - time4));
long time5 = System.currentTimeMillis();
copyFileByChannel(source, target + "5.txt");
System.out.println("通过JAVA NIO通道(非直接缓冲区)实现文件的拷贝耗时:" + (System.currentTimeMillis() - time5));
long time6 = System.currentTimeMillis();
copyFileByChannelBufferd(source, target + "6.txt");
System.out.println("通过JAVA NIO通道(直接缓冲区)实现文件的拷贝耗时:" + (System.currentTimeMillis() - time6));
long time7 = System.currentTimeMillis();
copyFileByChannelTransfer(source, target + "7.txt");
System.out.println("通过JAVA NIO通道传输实现文件的拷贝耗时:" + (System.currentTimeMillis() - time7));
long time8 = System.currentTimeMillis();
copyFileByChannelTransfer(source, target + "8.txt");
System.out.println("通过JAVA NIO通道传输2实现文件的拷贝耗时:" + (System.currentTimeMillis() - time8));
作者最新文章
赤友清理大师
2026-09-16 17:43
南邮光擎智算团队:GaN基Micro-LED光计算芯片从理论到流片的突破
2026-09-08 18:35
多张照片怎么合成PDF文件?三种图片转PDF工具怎么选?
2026-09-03 17:04
Excel转PDF防乱版指南:在线与本地双方案及排版检查
2026-09-03 10:04
多个PDF怎么合并成一个?合并后顺序怎么检查?
2026-09-02 19:54
上一篇:
mysql查询区分大小写吗
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















