q*s
2 楼
有一个很大的文件,不可能一次全读到计算机中,想随机地读出其中的某一行,大家有
什么好的算法么?
什么好的算法么?
R*G
3 楼
这个不用种了吧,直接下肚就行。
q*s
5 楼
多谢!我的意思是用java来实现,不是借助外部的工具。
b*y
6 楼
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);
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
7 楼
咋样?
q*s
9 楼
谢谢Briteguy的详细介绍,这也是我想到的,问题是需要更快。我想到下列一些方法:
1、把这些行倒到数据库中
2、把大文件分裂为小文件
3、建立index(谢谢googbug)
1、把这些行倒到数据库中
2、把大文件分裂为小文件
3、建立index(谢谢googbug)
h*c
10 楼
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?
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
12 楼
这个可以读一次解决。
基本办法是,
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 的大作中提到】
: 有一个很大的文件,不可能一次全读到计算机中,想随机地读出其中的某一行,大家有
: 什么好的算法么?
基本办法是,
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 的大作中提到】
: 有一个很大的文件,不可能一次全读到计算机中,想随机地读出其中的某一行,大家有
: 什么好的算法么?
q*s
14 楼
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?
【在 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?
相关阅读
有没有人熟悉tomcat?急!jboss deployment issue, again大家写java class的时候是完全封装的么?9191请问java如何画在坐标平面上的曲线?请帮忙看看这个编译错误再问generic问题:tomcat编译错误http post binding in wsdlPls recommend a free SOAP monitor.考SCEA归来Re: [转载] Questions on failover & Hot depRe: java.lang.reflect.InvocationTargetExJAVA XML Question关于Protected变量deploy j2me midlet to motorola v600How to set tab set of PRE in JEditorPane?what wrong with jboss?问一个generic的问题吧[转载] 谁能说说这两道Amazon的面试题Re: 考JCEA归来