簡體   English   中英

有沒有辦法從Java中的內存映射文件中獲取對象類型

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

我想將屬於不同類的對象存儲在單個內存映射文件中。 從文件讀取時,可以使用任何方法來了解對象類型。

內存映射文件僅用於單個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();
}

如果您使用自定義方法來序列化對象,則可以在所寫內容中包含開始和結束字節或某種格式。 然后,知道了它是如何編寫的,您可以創建一種方法來讀取該特定格式的文件。 當然,與僅使用標准Java序列化庫相比,這將需要更多的工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM