java_IO流多行数字读取统计小练习

  1. 读取文件"F:\Case\123.txt"中数字并选择合适的容器装起来,统计其个数并打印出来:
    123214
    424214
    242142
    123214
    123214
    214244
package day01.woltcode01;
import java.io.*;
import java.util.*;

public class TestDemo {
    public static void main(String[] args) throws Exception {
        List<String> list = new ArrayList<>();
        Set<String> set = new HashSet<>();
        int countList = 0;
        int countSet = 0;
        File file = new File("F:\\Case\\123.txt");
        FileReader reader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(reader);
        String string = null;
        while ((string = bufferedReader.readLine())!=null){
            if (string.charAt(0)=='1'&&string.charAt(1)=='2'){
                list.add(string);
                countList++;
            }else{
                set.add(string);
                countSet++;
            }
        }
        System.out.println(list.toString());
        System.out.println(set.toString());
        System.out.println(countList);
        System.out.println(countSet);
    }
}
package demo01;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.*;

public class Demo01 {
    public static void main(String[] args) throws Exception{
        //创建读取源
        File file = new File("F:\\Case\\123.txt");
        Reader reader = new InputStreamReader( new FileInputStream(file));
        //IO操作
        char[] chars = new char[1024];
        int len = reader.read(chars);
        reader.close();
        //接收字符串容器
        List<String> list = new ArrayList<>();
        Set<String> set = new HashSet<>();
        int countList = 0;
        int countSet = 0;
        //以下算法演示(参考)
        for (int i  = 0;i<len/8;i++) {
            if (Integer.valueOf(new String(chars,(i*8),1))==1){
                list.add(new String(chars,(i*8),6));
                countList++;
            }else {
                set.add(new String(chars,(i*8),6) );
                countSet++;
            }
        }
        System.out.println(list.toString());
        System.out.println(set.toString());
        System.out.println(countList);
        System.out.println(countSet);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值