要获得第N行,您必须阅读它之前的所有行.
如果不止一次这样做,最有效的方法可能是首先将所有行加载到内存中.
private final List words = new ArrayList();
private final Random random = new Random();
public String randomWord() throws IOException {
if (words.isEmpty()) {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("../image/ListeMot.txt")));
String line;
while ((line = br.readLine()) != null)
words.add(line);
br.close();
}
return words.get(random.nextInt(words.size()));
}
BTW:theWord的参数是什么意思?