zoukankan      html  css  js  c++  java
  • IO流的练习1 —— 随机获取文本中的信息

      需求:一个文本中有几个名字,随机从中获取一个名字
      分析:
        A:首先把文本中的数据读出
        B:再把数据存储到集合中
        C:产生一个随机的索引
        D:打印出这个索引对应的值

     1     public static void main(String[] args) throws IOException {
     2         //创建字符输入流对象
     3         BufferedReader br = new BufferedReader(new FileReader("name.txt"));
     4         //创建集合
     5         ArrayList<String> name = new ArrayList<String>();
     6         //读取文本数据
     7         String line = null;
     8         while((line = br.readLine()) != null){
     9             name.add(line);
    10         }
    11         br.close();
    12         
    13         //获取随机索引
    14         Random r = new Random();
    15         int index = r.nextInt(name.size());
    16         
    17         //获取对应的名字
    18         String result = name.get(index);
    19         System.out.println(result);
    20     }
    何事都只需坚持.. 难? 维熟尔。 LZL的自学历程...只需坚持
  • 相关阅读:
    操作系统典型调度算法
    C++ volatile 关键字
    vue class绑定 组件
    yarn 基本用法
    vscode 插件安装以及首选项配置
    git 多人协作
    git Feature分支
    git Bug分支
    git 分支策略
    git 解决冲突
  • 原文地址:https://www.cnblogs.com/LZL-student/p/5927190.html
Copyright © 2011-2022 走看看