简体   繁体   English

Android:在文件中定位(类似于C ++中的fseek)?

[英]Android: Positioning in file (similar to fseek in C++)?

I'm trying to read some data from html file that I opened from internet using httpClient. 我正在尝试从使用httpClient从Internet打开的html文件中读取一些数据。 I'm using readLine() function in loop for reading. 我在循环中使用readLine()函数进行读取。

The lines I want to read start at 500th row. 我要阅读的行从第500行开始。 Is there a faster way to position at file? 有没有更快的定位文件的方法?

Problem is that reading all that unnecessary data takes too long (almost 10 seconds to finish loading all). 问题在于,读取所有不必要的数据会花费太长时间(将近10秒钟才能完成全部加载)。

在InputStream中,您可以使用skip(long n)许多字符

You'll have to cross 499 line terminating characters to get to the 500th line (as getline() gets a line terminated by '\\n' , '\\r' or "\\r\\n" ). 您必须越过499行终止字符才能到达第500行(因为getline()获得以'\\n''\\r'"\\r\\n"终止的行)。

The definition of a line is the basic problem. 线的定义是基本问题。 As a line is terminated using the above terminators(standard definition in most languages), for any algorithm that skips n lines, the algorithm will need to discover n line terminators. 由于使用上述终止符(大多数语言中的标准定义)来终止行,因此对于跳过n行的任何算法,该算法将需要发现n行终止符。 This means that the input stream will need to be examined byte/char by byte/char to discover the terminators, which leads to the conclusion that while you can only skip characters, but you cannot skip lines . 这意味着输入流将需要检查byte/charbyte/char来发现终止,这导致的结论是,虽然你只能跳过字符,但你不能跳过 Here skipping means that data need not be examined (either in your code or in the library code), for eg. 在这里跳过意味着不需要检查数据(在您的代码或库代码中),例如。 a random access file type seek() or a File Stream skip(n) operation. 随机访问文件类型seek()或File Stream skip(n)操作。

Thus, you might want to rethink your data structures, or the need to skip lines. 因此,您可能想重新考虑数据结构,或者需要跳过行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM