如何通过JAVA NIO非直接缓冲区拷贝文件
作者:归人云淡风轻
时间:2023-05-04
来源:正软商城
浏览:4
通过JAVANIO非直接缓冲区拷贝文件/***通过JAVANIO非直接缓冲区拷贝文件**@paramsourcePath源文件路径*@paramtargetPath目标文件路径*/publicstaticvoidcopyFileByChannel(StringsourcePath,StringtargetPath){FileChanneloutChannel=null;FileChannelinChannel=null;FileInputStreamfis=null;FileOutputStreamfos
本文目录
通过JAVA NIO 非直接缓冲区拷贝文件
/**
* 通过JAVA NIO 非直接缓冲区拷贝文件
*
* @param sourcePath 源文件路径
* @param targetPath 目标文件路径
*/
public static void copyFileByChannel(String sourcePath, String targetPath) {
FileChannel outChannel = null;
FileChannel inChannel = null;
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(sourcePath);
fos = new FileOutputStream(targetPath);
//获取通道
inChannel = fis.getChannel();
outChannel = fos.getChannel();
//分配指定大小的缓冲区
ByteBuffer buf = ByteBuffer.allocate(1024);
while (inChannel.read(buf) != -1) {
//转换为读取数据模式
buf.flip();
//写入到磁盘
outChannel.write(buf);
//清空缓冲区
buf.clear();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭流
try {
if (outChannel != null) {
outChannel.close();
}
if (inChannel != null) {
inChannel.close();
}
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
作者最新文章
coreldraw快捷键怎么设置在哪里
2026-11-02 21:39
AO3最新可用链接及中文入口汇总
2026-10-29 13:32
coreldraw怎么插入图片 图片导入步骤
2026-10-27 15:37
coreldraw快捷键冲突 怎么改 自定义按键解决
2026-10-24 08:50
AO3中文界面设置指南与核心功能使用教程
2026-10-14 23:28
热门文章
更多
精品专题
更多
Mac软件
更多
火山引擎 网页版
Windows/ macOS/ Linux
火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。
















