The file has n line, so the probability of picking the kth line is 1 / k * k / k+1 * … * n-1 / n = 1 / n
int k;
int i = 1;
String s = f.readLine(); //assume at least one line
while (true) {
String next = f.readLine();
if (next == null) return s;
k = random(i + 1);
if (k == 0) // 1/k
return next;
s = next;
i++;
}