简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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