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

例如
圖中的一個 .csv 文件,需要讀取數(shù)據(jù)封裝對象進行數(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; //用來跳過第一行的名稱
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)有多個對象時
可以傳入一個 Class對象來獲取到需要封裝對象的類名,進一步實現(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();
//如果文件中存儲的是 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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot如何使用mybatis實現(xiàn)攔截SQL分頁
這篇文章主要介紹了Springboot使用mybatis實現(xiàn)攔截SQL分頁,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
Spring事務(wù)注解@Transactional失效的八種場景分析
最近在開發(fā)采用Spring框架的項目中,使用了@Transactional注解,但發(fā)現(xiàn)事務(wù)注解失效了,所以這篇文章主要給大家介紹了關(guān)于Spring事務(wù)注解@Transactional失效的八種場景,需要的朋友可以參考下2021-05-05
Spring session實現(xiàn)Session共享
本文主要介紹了Spring session實現(xiàn)Session共享,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Spring Boot security 默認(rèn)攔截靜態(tài)資源的解決方法
這篇文章主要介紹了Spring Boot security 默認(rèn)攔截靜態(tài)資源,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
SpringBoot整合rabbitMq自定義消息轉(zhuǎn)換方式
這篇文章主要介紹了SpringBoot整合rabbitMq自定義消息轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
Spring Cloud基于zuul實現(xiàn)網(wǎng)關(guān)過程解析
這篇文章主要介紹了Spring Cloud基于zuul實現(xiàn)網(wǎng)關(guān)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12

