如何通过JAVA NIO直接缓冲区拷贝文件
作者:暮色微凉
时间:2023-04-27
来源:互联网
浏览:0
通过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();
}
}
}
作者最新文章
白描 PDF
2026-09-16 17:44
密码键盘
2026-09-16 17:43
3dmax快捷键失效了怎么办
2026-09-16 13:53
Xiaomi 18 Fold首销数据解读:较上代大折叠增长310%的原因与配置分析
2026-09-08 16:55
PDF文件太大怎么压缩?在线减小体积的操作步骤
2026-09-03 11:12
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















