由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 随机读一个大文件中的任意一行
相关主题
如何处理中文文件名?请问同时执行几个bat文件的问题
请问个BufferedReader 读 file 的问题JAVA文本文件读写问题
急请教:用java实现解析parse一个log文件,多谢指点问一个blocking IO的程序
怎样截取网页
中多个

之间的内容?
新手求教 BufferedReader.readLine()
怎么从键盘输入整数或float?how to use grep/sed to remove newlines? (转载)
[转载] smtp server in java?System.in如何使用UTF-8?
新手请教怎样在Java里读文本文件中的内容Re: How can I call another program from Java?
请教问题,怎么确定空行!Re: Need Emergent help for Java I/O!
相关话题的讨论汇总
话题: line话题: random话题: numlines话题: threshhold话题: result
进入Java版参与讨论
1 (共1页)
q***s
发帖数: 2243
1
有一个很大的文件,不可能一次全读到计算机中,想随机地读出其中的某一行,大家有
什么好的算法么?
g**e
发帖数: 6127
2
google reservoir sampling

【在 q***s 的大作中提到】
: 有一个很大的文件,不可能一次全读到计算机中,想随机地读出其中的某一行,大家有
: 什么好的算法么?

q***s
发帖数: 2243
3
多谢!我的意思是用java来实现,不是借助外部的工具。
b******y
发帖数: 9224
4
Random rand = new Random();
BufferedReader reader = new BufferedReader(new FileReader("bigfile.txt"));
// get the number of lines
int numLines = 0;
while (true)
{
String line = reader.readLine();
if (line == null) break;
numLines++;
}
// second pass, randomly get a line
String line = null;
float threshHold = 1 / (float) numLines;
for (int i = 0; i < numLines; i++)
{
line = reader.readLine();
if (rand.nextFloat() < threshHold)
{
// we got the line, break
break;
}
}
System.out.println("the random line is: " + line);
b******y
发帖数: 9224
5
咋样?
g*****g
发帖数: 34805
6
If you really want to do it quickly, you should build an index.

【在 q***s 的大作中提到】
: 有一个很大的文件,不可能一次全读到计算机中,想随机地读出其中的某一行,大家有
: 什么好的算法么?

q***s
发帖数: 2243
7
谢谢Briteguy的详细介绍,这也是我想到的,问题是需要更快。我想到下列一些方法:
1、把这些行倒到数据库中
2、把大文件分裂为小文件
3、建立index(谢谢googbug)
h**********c
发帖数: 4120
8
This is more like a operating system question.
the file has a size,
postion = rand (sizeofile);
sizeoffile <=0 error
put file pointer to position,
I. read forward two '\n' '\n', the line should be in between.
II. Or read backward two '\n' '\n' if not I.
III. If neither I nor II,...
should O(1).
btw, do you mean, read to memory at once?
b******y
发帖数: 9224
9

你倒到数据库的时间比读取一次文件的时间多多了,不划算。
我想到的另一个办法就是,可能统计上有点技巧,比如说, 第一行的时候,几率的
threshhold小,然后,随着读取的行数增加,threshhold不断增加。
但问题是不知道行数,所以,好像不容易设置dynamic的threshhold...

【在 q***s 的大作中提到】
: 谢谢Briteguy的详细介绍,这也是我想到的,问题是需要更快。我想到下列一些方法:
: 1、把这些行倒到数据库中
: 2、把大文件分裂为小文件
: 3、建立index(谢谢googbug)

m****r
发帖数: 6639
10
这个可以读一次解决。
基本办法是,
count = 0;
result = null;
while (true) {
line = readLine();
if line == null {
break;
} else {
result = random(1,count) == 1? line : result;
}
}
result = random(1,count) == 1? line : result;
return result;
差不多就是这个意思。 我的code肯定有错, 倒是。

【在 q***s 的大作中提到】
: 有一个很大的文件,不可能一次全读到计算机中,想随机地读出其中的某一行,大家有
: 什么好的算法么?

g**e
发帖数: 6127
11
then it's not strictly random distribution

【在 b******y 的大作中提到】
:
: 你倒到数据库的时间比读取一次文件的时间多多了,不划算。
: 我想到的另一个办法就是,可能统计上有点技巧,比如说, 第一行的时候,几率的
: threshhold小,然后,随着读取的行数增加,threshhold不断增加。
: 但问题是不知道行数,所以,好像不容易设置dynamic的threshhold...

q***s
发帖数: 2243
12
cool, this should be the best one, I ever met

【在 h**********c 的大作中提到】
: This is more like a operating system question.
: the file has a size,
: postion = rand (sizeofile);
: sizeoffile <=0 error
: put file pointer to position,
: I. read forward two '\n' '\n', the line should be in between.
: II. Or read backward two '\n' '\n' if not I.
: III. If neither I nor II,...
: should O(1).
: btw, do you mean, read to memory at once?

m****r
发帖数: 6639
13
but this clearly is not uniformedly distributed. longer lines has high
chance of getting picked.

【在 q***s 的大作中提到】
: cool, this should be the best one, I ever met
1 (共1页)
进入Java版参与讨论
相关主题
Re: Need Emergent help for Java I/O!怎么从键盘输入整数或float?
请教一个问题,thanks![转载] smtp server in java?
Java的中文读写问题新手请教怎样在Java里读文本文件中的内容
GUI THANKS请教问题,怎么确定空行!
如何处理中文文件名?请问同时执行几个bat文件的问题
请问个BufferedReader 读 file 的问题JAVA文本文件读写问题
急请教:用java实现解析parse一个log文件,多谢指点问一个blocking IO的程序
怎样截取网页
中多个

之间的内容?
新手求教 BufferedReader.readLine()
相关话题的讨论汇总
话题: line话题: random话题: numlines话题: threshhold话题: result