J2ME中的文件随机访问 [英] File random access in J2ME

查看:58
本文介绍了J2ME中的文件随机访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

J2ME是否具有与 类,还是有什么方法可以模仿这种特殊的(随机访问)功能?

does J2ME have something similar to RandomAccessFile class, or is there any way to emulate this particular (random access) functionality?

问题是这样的:我有一个相当大的二进制数据文件(〜600 KB),并且想创建一个使用该数据的移动应用程序.该数据的格式是自制的,并且包含许多索引块和数据块.在其他平台(例如PHP或C)上读取数据通常是这样的:

The problem is this: I have a rather large binary data file (~600 KB) and would like to create a mobile application for using that data. Format of that data is home-made and contains many index blocks and data blocks. Reading the data on other platforms (like PHP or C) usually goes like this:

  1. 读取2个字节的索引键(K),再读取2个字节的索引值(V)以获取所需的数据类型
  2. 从文件的开头跳过V个字节,以查找到索引键K的数据开始的文件位置
  3. 读取数据
  4. 利润:)
  1. Read 2 bytes for index key (K), another 2 for index value (V) for the data type needed
  2. Skip V bytes from the start of the file to seek to a file position there the data for index key K starts
  3. Read the data
  4. Profit :)

在程序流程中,这会发生很多次.

This happens many times during the program flow.

嗯,我正在研究在J2ME上做同样的事情的可能性,尽管我承认我对整个Java还是很陌生,但我似乎无法找到除 InputStream (

Um, and I'm investigating possibility of doing the very same on J2ME, and while I admit I'm quite new to the whole Java thing, I can't seem to be able to find anything beyond InputStream (DataInputStream) classes which don't have the basic seeking/skipping to byte/returning position functions I need.

那么,我有什么机会?

推荐答案

您应该有这样的内容

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();
}

我不想使用marke/reset方法,我认为最好将val位置的偏移量而不是文件的开头保存下来,因此可以跳过这些方法.我认为他们在某些设备上有som问题.

I prefer not to use the marke/reset methods, I think it is better to save the offset from the val location not from the start of the file so you can skip these methods. I think they have som issues on some devices.

请注意,我不建议打开600 KB文件,它会使许多低端设备上的应用程序崩溃,您应该将此文件拆分为多个文件.

One more note, I don't recommend to open a 600 KB file, it will crash the application on many low end devices, you should split this file to multiple files.

这篇关于J2ME中的文件随机访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆