分析: Random属于集合的方法
1、把文本文件存储到集合中
2、随机产生一个索引
3、根据索引获取一个值即可得到
代码
package cn.idcast4;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
public class Day1103 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("b.txt"));
ArrayList<String> array = new ArrayList<String>();
String line = null;
while ((line = br.readLine()) != null) {
array.add(line);
}
br.close();
Random r = new Random();
int index = r.nextInt(array.size()); 确认索引的长度
String name = array.get(index);
System.out.println("该幸运者是:" + name);
}
}