gpt4 book ai didi

java - 用java.nio在内存中写入文件?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:26:43 25 4
gpt4 key购买 nike

使用 nio 可以映射内存中的现有文件。但是是否可以只在内存中创建它而不在硬盘上创建文件?

我想模仿允许您在内存中写入的 CreateFileMapping 窗口函数。

Java 中是否有等效的系统?

目标是写入内存以便另一个程序 ( c ) 读取它。

最佳答案

请看下面的内容。已创建一个文件,但这可能与您即将获得的文件一样接近。

MappedByteBuffer
MappedByteBuffer.load()
FileChannel
FileChannel.map()

这里有一个片段可以帮助您入门。

    filePipe = new File(tempDirectory, namedPipe.getName() + ".pipe");
try {
int pipeSize = 4096;
randomAccessFile = new RandomAccessFile(filePipe, "rw");
fileChannel = randomAccessFile.getChannel();
mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, pipeSize);
mappedByteBuffer.load();
} catch (Exception e) {
...

关于java - 用java.nio在内存中写入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9827481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com