Random file access in J2ME

Does J2ME have something similar to the RandomAccessFile class, or is there a way to emulate this specific functionality (random access)?

The problem is this: I have a rather large binary data file (~ 600 KB) and you want to create a mobile application to use this data. The format of this data is self-made and contains many index blocks and data blocks. Reading data on other platforms (e.g. PHP or C) usually happens as follows:

  • Read 2 bytes for the index key ( K ), another 2 for the index value ( V ) for the required data type
  • Skip V bytes from the beginning of the file to search for the file position where the data for the K keyword begins
  • Read data
  • Profit:)

This happens many times during a program flow.

Um, and I am exploring the possibility of doing the same thing in J2ME, and although I admit that I am completely new to all Java stuff, I can not find anything other than InputStream ( DataInputStream ) classes that do not have a basic search / skip byte / return function of the position that I need.

So what are my chances?

+2
source share
1 answer

You should have something like this

 try { DataInputStream di = new DataInputStream(is); di.marke(9999); short key = di.readShort(); short val = di.readShort(); di.reset(); di.skip(val); byte[] b= new byte[255]; di.read(b); }catch(Exception ex ) { ex.printStackTrace(); } 

I prefer not to use marke / reset methods, I think it’s better to keep the offset from the val location not from the beginning of the file so you can skip these methods. I think they have some problems on some devices.

One more note: I do not recommend opening a file with a size of 600 KB, this will lead to a crash of the application on many devices with low access level, you should split this file into several files.

+2
source

Source: https://habr.com/ru/post/890820/


All Articles