【从文本里随机获取一个人名】

package com.companyname.common.test;

import java.io.*;
import java.util.ArrayList;
import java.util.Random;

/**
 * @Description
 * @Author Created by shusheng.
 * @Email [email protected]
 * @Date 2018/12/8
 */
public class GetName {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new FileReader("a.txt"));
        ArrayList<String> array = new ArrayList<String>();

        String line = null;
        while((line=br.readLine())!=null){
            array.add(line);
        }

        Random r = new Random();
        int index = r.nextInt(array.size());

        String name = array.get(index);
        System.out.println("幸运者是:"+name);
    }

}

猜你喜欢

转载自www.cnblogs.com/zuixinxian/p/10086995.html