本文介绍了使用java.nio在内存中写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用nio可以将现有文件映射到内存中。但是有可能只在内存中创建它而没有硬盘上的文件吗?
With nio it is possible to map an existing file in memory. But is it possible to create it only in memory without file on the hard drive ?
我想模仿CreateFileMapping窗口函数,它允许你在内存中写入。
I want to mimic the CreateFileMapping windows functions which allow you to write in memory.
Java中是否有一个等效的系统?
Is there an equivalent system in Java ?
目标是在内存中写入另一个程序(c )阅读它。
The goal is to write in memory in order for another program ( c ) to read it.
推荐答案
看看以下内容。创建了一个文件,但这可能与您要获得的文件一样接近。
Have a look at the following. A file is created but this might be as close as your going to get.
MappedByteBuffer
MappedByteBuffer.load()
FileChannel
FileChannel.map()
这是一个尝试的片段让你开始吧
Here is a snippet to try and get you started.
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.nio在内存中写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!