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的方法)?可以教教我吗?