java读取txt文件内容

一、问题背景

有一个txt文件,需要按行读取内容,并按逗号分隔

 二、上代码

public class Main {
    public static void main(String[] args) throws IOException {
        List<Flow> flows = new ArrayList<Flow>();
        InputStream f1 = new FileInputStream("./data/flow_test.txt");
        InputStreamReader reader = new InputStreamReader(f1);
        BufferedReader br = new BufferedReader(reader);
        br.readLine();  //读取第一行且不作处理
        String strTmp = "";
        while ((strTmp = br.readLine()) != null) {
            String[] values = strTmp.split(",");
            int flow_id = Integer.parseInt(values[0]), flow_bw = Integer.parseInt(values[1]);
            int start = Integer.parseInt(values[2]), cost = Integer.parseInt(values[3]);
            Flow flow = new Flow(flow_id, flow_bw, start, cost);
            flows.add(flow);
        }
        br.close();
        System.out.println(flows.size());
    }
}

class Flow {
    private int id; //流id
    private int bw; //流带宽
    private int start;  //起始时间
    private int cost;   //发送时间

    public Flow(int id, int bw, int start, int cost) {
        this.id = id;
        this.bw = bw;
        this.start = start;
        this.cost = cost;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getBw() {
        return bw;
    }

    public void setBw(int bw) {
        this.bw = bw;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getCost() {
        return cost;
    }

    public void setCost(int cost) {
        this.cost = cost;
    }
}

三、输出结果

共19条数据,故输出19。

大家还有什么好的读取方法吗(有没有直接读取int的方法)?可以教教我吗?

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值