由于java字节码文件的限制,类文件不能大于64k iirc。(它们根本不适用于此类数据。
我会在启动程序时加载数据,使用类似于以下代码行的内容:
import java.io.*;
import java.util.*;
public class Test {
public static void main(String... args) throws IOException {
List<DataRecord> records = new ArrayList<DataRecord>();
BufferedReader br = new BufferedReader(new FileReader("data.txt"));
String s;
while ((s = br.readLine()) != null) {
String[] arr = s.split(" ");
int i = Integer.parseInt(arr[0]);
int j = Integer.parseInt(arr[1]);
records.add(new DataRecord(i, j, arr[0]));
}
}
}
class DataRecord {
public final int i, j;
public final String s;
public DataRecord(int i, int j, String s) {
this.i = i;
this.j = j;
this.s = s;
}
}
(铌:扫描仪非常慢,所以不要仅仅因为它有一个简单的界面就试图使用它。坚持使用某种形式的 BufferedReader 和 split,或 StringTokenizer。
当然,如果将数据转换为二进制格式,则可以提高效率。在这种情况下,您可以使用(但不要忘记通过一些或DataInputStream
BufferedInputStream
BufferedReader
)
根据您希望访问数据的方式,最好将记录存储在哈希映射()中(具有或作为键)。HashMap<Integer, DataRecord>
i
j
如果您希望在JVM加载类文件本身的同时加载数据(大致上!),则可以执行读取/初始化,不是在方法中,而是在.static { ... }
对于内存映射方法,请查看 java 中的 -package。特别是方法java.nio.channels
public abstract MappedByteBuffer map(FileChannel.MapMode mode, long position,long size) throws IOException
可在此处找到完整的代码示例。
Dan Bornstein(DalvikVM的首席开发人员)在本次演讲中解释了您的问题的解决方案(请看0:30:00)。但是,我怀疑该解决方案适用于与一兆字节一样多的数据。