1. 程式人生 > >【從文本里隨機獲取一個人名】

【從文本里隨機獲取一個人名】

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); } }