java讀取txt文件內(nèi)容簡單舉例
一、問題背景
有一個txt文件,需要按行讀取內(nèi)容,并按逗號分隔
二、上代碼
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; //發(fā)送時間 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; } }
三、輸出結(jié)果
共19條數(shù)據(jù),故輸出19。
大家還有什么好的讀取方法嗎(有沒有直接讀取int的方法)?
總結(jié)
到此這篇關(guān)于java讀取txt文件內(nèi)容的文章就介紹到這了,更多相關(guān)java讀取txt文件內(nèi)容內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot中restful風(fēng)格請求的使用方法示例
RESTful是一種web軟件風(fēng)格,它不是標(biāo)準(zhǔn)也不是協(xié)議,它不一定要采用,只是一種風(fēng)格,它倡導(dǎo)的是一個資源定位(url)及資源操作的風(fēng)格,下面這篇文章主要給大家介紹了關(guān)于springboot中restful風(fēng)格請求的使用方法,需要的朋友可以參考下2023-02-02Java定時調(diào)用.ktr文件的示例代碼(解決方案)
這篇文章主要介紹了Java定時調(diào)用.ktr文件的示例代碼,本文給大家分享遇到問題及解決方法,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04分布式調(diào)度器之Spring Task 的使用詳解
SpringTask是Spring框架中用于任務(wù)調(diào)度的組件,通過簡單的注解就能實現(xiàn)定時任務(wù)的創(chuàng)建和調(diào)度,可以通過配置線程池來實現(xiàn),本文給大家介紹分布式調(diào)度器之Spring Task 的使用,感興趣的朋友跟隨小編一起看看吧2024-10-10myeclipse智能提示設(shè)置的實現(xiàn)方法
本篇文章介紹了,myeclipse智能提示設(shè)置的實現(xiàn)方法。需要的朋友參考下2013-05-05java 開發(fā)中網(wǎng)絡(luò)編程之IP、URL詳解及實例代碼
這篇文章主要介紹了java 開發(fā)中網(wǎng)絡(luò)編程之IP、URL詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03