Java如何讀取csv文件并將數(shù)據(jù)放入對(duì)象中
讀取csv文件并封裝數(shù)據(jù)為對(duì)象

例如
圖中的一個(gè) .csv 文件,需要讀取數(shù)據(jù)封裝對(duì)象進(jìn)行數(shù)據(jù)持久化。
public static void readCSV(String readpath, ArrayList list)
{
File inFile = new File(readpath);
try
{
BufferedReader reader = new BufferedReader(new FileReader(inFile));
boolean sign = false; //用來(lái)跳過(guò)第一行的名稱
while(reader.ready())
{
String line = reader.readLine();
StringTokenizer st = new StringTokenizer(line, ",");
int date, time, num_transaction, response_time;
double sucRate;
if (st.hasMoreTokens() && sign)
{
date = Integer.valueOf(st.nextToken().trim());
time = Integer.valueOf(st.nextToken().trim());
num_transaction = Integer.valueOf(st.nextToken().trim());
sucRate = Double.valueOf(st.nextToken().trim());
response_time = Integer.valueOf(st.nextToken().trim());
Sample sample = new Sample(date, time, num_transaction, sucRate, response_time);
list.add(sample);
}
else
{
sign = true;
}
}
reader.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}當(dāng)有多個(gè)對(duì)象時(shí)
可以傳入一個(gè) Class對(duì)象來(lái)獲取到需要封裝對(duì)象的類名,進(jìn)一步實(shí)現(xiàn)方法一般化:
public class ReadCSV {
public static void readCSV(InputStream inputStream, ArrayList<Object> list, Class cls){
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(inputStream));
boolean flag = false;
ArrayList<String> headerList = new ArrayList();
while(reader.ready()){
String line = reader.readLine();
StringTokenizer st = new StringTokenizer(line,",");
//處理當(dāng)前行數(shù)據(jù)
if(st.hasMoreTokens() && flag){
String typeName = cls.getSimpleName();
//如果文件中存儲(chǔ)的是 EnergyProvince類信息
if(typeName.equals("EnergyProvice")){
String provinceName = st.nextToken();
// Float year2019 = Float.valueOf(st.nextToken());
// Float year2018 = Float.valueOf(st.nextToken());
Float year2017 = Float.valueOf(st.nextToken());
Float year2016 = Float.valueOf(st.nextToken());
Float year2015 = Float.valueOf(st.nextToken());
Float year2014 = Float.valueOf(st.nextToken());
Float year2013 = Float.valueOf(st.nextToken());
Float year2012 = Float.valueOf(st.nextToken());
Float year2011 = Float.valueOf(st.nextToken());
Map<String,Float> dataMap = new HashMap();
// dataMap.put(headerList.get(1),year2019);
// dataMap.put(headerList.get(2),year2018);
dataMap.put(headerList.get(1),year2017);
dataMap.put(headerList.get(2),year2016);
dataMap.put(headerList.get(3),year2015);
dataMap.put(headerList.get(4),year2014);
dataMap.put(headerList.get(5),year2013);
dataMap.put(headerList.get(6),year2012);
dataMap.put(headerList.get(7),year2011);
list.add(new EnergyProvice(provinceName,dataMap));
}
}
else{ //添加表頭到 List 集合
while(st.hasMoreTokens()){
headerList.add(st.nextToken());
}
flag=true;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(reader!=null)
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot如何使用mybatis實(shí)現(xiàn)攔截SQL分頁(yè)
這篇文章主要介紹了Springboot使用mybatis實(shí)現(xiàn)攔截SQL分頁(yè),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Spring事務(wù)注解@Transactional失效的八種場(chǎng)景分析
最近在開(kāi)發(fā)采用Spring框架的項(xiàng)目中,使用了@Transactional注解,但發(fā)現(xiàn)事務(wù)注解失效了,所以這篇文章主要給大家介紹了關(guān)于Spring事務(wù)注解@Transactional失效的八種場(chǎng)景,需要的朋友可以參考下2021-05-05
Spring session實(shí)現(xiàn)Session共享
本文主要介紹了Spring session實(shí)現(xiàn)Session共享,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Spring Boot security 默認(rèn)攔截靜態(tài)資源的解決方法
這篇文章主要介紹了Spring Boot security 默認(rèn)攔截靜態(tài)資源,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
SpringBoot整合rabbitMq自定義消息轉(zhuǎn)換方式
這篇文章主要介紹了SpringBoot整合rabbitMq自定義消息轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Spring Cloud基于zuul實(shí)現(xiàn)網(wǎng)關(guān)過(guò)程解析
這篇文章主要介紹了Spring Cloud基于zuul實(shí)現(xiàn)網(wǎng)關(guān)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
SWT(JFace) 體驗(yàn)之FontRegistry
測(cè)試代碼如下:2009-06-06
java的Builder原理和實(shí)現(xiàn)詳解
大家好,本篇文章主要講的是java的Builder原理和實(shí)現(xiàn)詳解,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12

