代码如下:
得先在路径下创建txt文件,输入名字
package com;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
public class dianName {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("test.txt"));
ArrayList<String> arry=new ArrayList<String>();
String line;
while ((line=br.readLine())!=null){
arry.add(line);
}
br.close();
Random r=new Random();
int index = r.nextInt(arry.size());
String name=arry.get(index);
System.out.println("幸运儿是:"+name+" 同志");
}
}