Java - 随机行读取 [英] Java - Random line read

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

问题描述

我正在编写一个Android应用程序,并希望我的程序读取一个文件的随机行。为了做到这一点,你需要固定长度的行(实现的细节应该是在这种情况下是显而易见的),或者关于它们有多少行以及(可选地,为了更好的性能)关于它们开始的文件内部偏移(排序索引)的信息。



对于小文件,只要需要随机行,就可以根据需要创建这样的索引。为了有效地处理大文件,你需要保持索引持续,也许在一个单独的文件。



如果线条大致相同的长度,你不要不需要完美的随机性,你也可以在文件中选择一个随机的字节偏移量,并扫描最近的换行符。


I am programing an Android app, and would like my program to read a random line of a file. How would I go about doing that?

解决方案

To do that, you need either fixed-length lines (the implementation details should be obvious in that case) or information about how many lines there are and (optionally, for better performance) at what offsets inside the file they begin (an index of sorts).

For small files, you can create such an index on demand whenever you need a random line. To do it efficiently for large files, you need to keep the index around persistently, perhaps in a separate file.

If lines tend to be roughly the same length and you don't need perfect "randomness", you could also pick a random byte offset inside the file and scan for the nearest line break.

这篇关于Java - 随机行读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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