简体   繁体   English

有没有办法从Java中的内存映射文件中获取对象类型

[英]Is there any way to get Object type from the memory mapped file in java

I want to store objects belonging to different classes in a single memory mapped file. 我想将属于不同类的对象存储在单个内存映射文件中。 While reading from the file is there any approach to know the object type. 从文件读取时,可以使用任何方法来了解对象类型。

Is memory mapped file is used for only single java class? 内存映射文件仅用于单个Java类吗?

try {    
    RandomAccessFile memoryMappedFile = new RandomAccessFile(file,mode);    
    long fileSize = memoryMappedFile.length();    
    System.out.println("file size " + fileSize);    
    FileChannel fc = memoryMappedFile.getChannel();    
    System.out.println("Chaannel size" + fc.size());    
    MappedByteBuffer outBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());    
    List<MemoryMappedFile> list = new ArrayList<MemoryMappedFile>();    
    while(outBuffer.hasRemaining()) {    
        MemoryMappedFile mf = new MemoryMappedFile(outBuffer.getLong(), outBuffer.getInt(), outBuffer.getDouble());        
        list.add(mf);    
    }
    fc.close();
    memoryMappedFile.close();
}

If you make a custom method to serialize your objects you can include starting and ending bytes or a certain format to what you write. 如果您使用自定义方法来序列化对象,则可以在所写内容中包含开始和结束字节或某种格式。 Then, knowing how it is written, you can make a method to read files of that specific format. 然后,知道了它是如何编写的,您可以创建一种方法来读取该特定格式的文件。 This would, of course, require more work than just using the standard Java serialization library though. 当然,与仅使用标准Java序列化库相比,这将需要更多的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM