如何通过JAVA NIO直接缓冲区拷贝文件
作者:暮色微凉
时间:2023-05-01
浏览:3
通过JAVANIO直接缓冲区拷贝文件/***通过JAVANIO直接缓冲区拷贝文件(内存映射文件)**@paramsourcePath源文件路径*@paramtargetPath目标文件路径*/publicstaticvoidcopyFileByChannelBufferd(StringsourcePath,StringtargetPath){FileChannelinChannel=null;FileChanneloutChannel=null;try{//获取通道,StandardOpenOption.
本文目录
通过JAVA NIO 直接缓冲区拷贝文件
/**
* 通过JAVA NIO 直接缓冲区拷贝文件(内存映射文件)
*
* @param sourcePath 源文件路径
* @param targetPath 目标文件路径
*/
public static void copyFileByChannelBufferd(String sourcePath, String targetPath) {
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
//获取通道,StandardOpenOption.READ表示可读,StandardOpenOption.WRITE表示可写,StandardOpenOption.CREATE表示可以创建
inChannel = FileChannel.open(Paths.get(sourcePath), StandardOpenOption.READ);
outChannel = FileChannel.open(Paths.get(targetPath), StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);
//创建内存映射文件
MappedByteBuffer inMapped = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
MappedByteBuffer outMapped = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());
//直接操作内存映射文件
byte[] buf = new byte[inMapped.limit()];
inMapped.get(buf);
outMapped.put(buf);
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
try {
if (outChannel != null) {
outChannel.close();
}
if (inChannel != null) {
inChannel.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
作者最新文章
漫蛙漫画最新版本下载
2026-10-13 13:20
漫蛙漫画官方免费漫画入口
2026-10-13 13:19
漫蛙漫画网页版入口
2026-10-13 13:19
最新漫蛙链接合集2026
2026-10-13 13:16
漫蛙manwa2登录网址
2026-10-13 13:14
下一篇:
如何使用注解实现Redis缓存功能
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多









































