Windows系统中的Java内存映射文件

问题描述 投票:0回答:1

我正在尝试新的东西,有一个应用程序将数据发送到位于Local \ MemFileName

的内存映射文件中

我想用Java阅读,

我尝试过一些教程,例如https://www.baeldung.com/java-mapped-byte-bufferhttps://howtodoinjava.com/java7/nio/memory-mapped-files-mappedbytebuffer/

但是所有人似乎都在JVM中读取文件,或者我听不懂...

如何读取位于Windows系统Local \ MemFileName中的文件的内容

谢谢!

以下:我尝试过的示例代码

public class Main {

private static final String IRSDKMEM_MAP_FILE_NAME = StringEscapeUtils.unescapeJava("Local\\IRSDKMemMapFileName");
private static final String IRSDKDATA_VALID_EVENT  = StringEscapeUtils.unescapeJava("Local\\IRSDKDataValidEvent");

public static final CharSequence charSequence = "Local\\IRSDKMemMapFileName";

public static void main(String[] args) throws IOException, InterruptedException {

    System.out.println(charSequence);

    try (RandomAccessFile file = new RandomAccessFile(new File(IRSDKMEM_MAP_FILE_NAME), "r")) {
        //Get file channel in read-only mode
        FileChannel fileChannel = file.getChannel();

        //Get direct byte buffer access using channel.map() operation
        MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());

        // the buffer now reads the file as if it were loaded in memory.
        System.out.println("Loaded " + buffer.isLoaded());  //prints false
        System.out.println("capacity" + buffer.capacity());  //Get the size based on content size of file

        //You can read the file from this buffer the way you like.
        for (int i = 0; i < buffer.limit(); i++) {
            System.out.println((char) buffer.get()); //Print the content of file
        }
    }


}

}

java memory-mapped-files
1个回答
0
投票

读取内存映射文件:

使用FileChannel在文件上打开FileChannel.open

map上调用FileChannel方法以创建一个MappedByteBuffer来覆盖要读取的文件区域。

MappedByteBuffer读取数据。

© www.soinside.com 2019 - 2024. All rights reserved.