随机读取txt文件的某一行,每一行的概率都是一样的
public static String choose(File f) throws FileNotFoundException
{
String result = null;
Random rand = new Random();
int n = 0;
for(Scanner sc = new Scanner(f); sc.hasNext(); )
{
++n;
String line = sc.nextLine();
System.out.println("n: "+n+" line: "+line);
if(rand.nextInt(n) == 0)
result = line;
}
return result;
}