java io 继承集合的功能实现

读取文件,拿到首字母大写的单词,存入集合

public class test2 extends ArrayList {//继承ArrayList,用他父类的构造方法
    public static void main(String[] args) throws IOException {
       //使用字符流读取一个文件,

        reader("G:\\\\Demo\\\\practice\\\\test2.txt");
        TreeSet<String> treeSet=new TreeSet<>(
                new test2("G:\\Demo\\practice\\test2.txt","\\W+")
        );
        System.out.println(treeSet.headSet("a"));
    }
  public static  String reader(String fileName) throws IOException {

      //使用字符流读取一个文件,
      StringBuilder stringBuilder =new StringBuilder();
      //字符输入流包装成缓冲流
      BufferedReader bf=null;
      try {
          bf=new BufferedReader(new FileReader(
                  //得到文件的绝对路径
                  new File(fileName).getAbsoluteFile())
          );
          String s;
          while((s=bf.readLine())!= null){
              stringBuilder.append(s);
              stringBuilder.append("\n");
              System.out.println(s);
              //将首字母大写的单词放入集合,然后按照26个英文字母的顺序排序

          }

      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }finally {
          bf.close();
      }
      return stringBuilder.toString();
  }
  //构造
  public test2(String fileName,String splite) throws IOException {
      super(Arrays.asList(reader(fileName).split(splite)));//调用ArrayList的父类构造,需要一个数组
      if (get(0).equals("")){
          remove(0);
      }

  }
}

读取文件 拿到首字母大写单词存入集合
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值