好处:
所以,我有这个二进制数据文件(大小 - 正好是640631字节),我正在尝试让Java读取它。
我有两个可互换的类实现为用于读取数据的层。其中一个使用RandomAccessFile
,效果很好。
糟糕:
另一个(这个问题主要是关于的)试图使用FileInputStream
和DataInputStream
,以便在MIDP 2.0(CLDC 1.1)上读取相同的数据(至少在理论上) Java配置(没有RandomAccessFile
)。
在该课程中,我打开这样的数据文件:
FileInputStream res = new FileInputStream(new File(filename));
h = new DataInputStream(res);
...并像这样实施seek()
/ skip()
(position
是long
,注意文件中的当前位置):
public void seek(long pos) throws java.io.IOException {
if (! this.isOpen()) {
throw new java.io.IOException("No file is open");
}
if (pos < position) {
// Seek to the start, then skip some bytes
this.reset();
this.skip(pos);
} else if (pos > position) {
// skip the remaining bytes until the position
this.skip(pos - position);
}
}
和
public void skip(long bytes) throws java.io.IOException {
if (! this.isOpen()) {
throw new java.io.IOException("No file is open");
}
long skipped = 0, step = 0;
do {
step = h.skipBytes((int)(bytes - skipped));
if (step < 0) {
throw new java.io.IOException("skip() failed");
}
skipped += step;
} while (skipped < bytes);
position += bytes;
}
丑陋:
第二个类(FileInputStream
/ DataInputStream
)的问题在于,有时它会决定将文件位置重置为文件中某个奇怪的位置:)当我运行这个时,会发生这种情况在J2SE(计算机)和J2ME(手机)上。以下是该读者类的实际用法示例以及发生的错误:
// Open the data file
Reader r = new Reader(filename);
// r.position = 0, actual position in a file = 0
// Skip to where the data block that is needed starts
// (determined by some other code)
r.seek(189248);
// r.position = 189248, actual position in a file = 189248
// Do some reading...
r.readID(); r.readName(); r.readSurname();
// r.position = 189332, actual position in a file = 189332
// Skip some bytes (an unneeded record)
r.skip(288);
// r.position = 189620, actual position in a file = 189620
// Do some more reading...
r.readID(); r.readName(); r.readSurname();
// r.position = 189673, actual position in a file = 189673
// Skip some bytes (an unneeded record)
r.skip(37);
// AAAAND HERE WE GO:
// r.position = 189710, actual position in a file = 477
我能够确定当要求跳过另外37个字节时,Java会将文件指针从一开始或文件定位到字节477.
“Freshly”(刚刚打开文件后)寻找位置189710(以及超出该位置)的工作正常。但是,每次需要seek()
时重新打开文件的速度都非常慢,尤其是在手机上。
发生了什么事?
答案 0 :(得分:0)
我认为这没有错。你最后一次跳过之前的r.position值是肯定的吗?除非JDK流中存在潜在的错误,或者如果您使用Reader有多个多线程,那么我能猜到的唯一可能是在读取字段时错误地修改了位置值。