国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

java讀取并導(dǎo)出多類型數(shù)據(jù)csv文件

2019-11-14 23:28:04
字體:
供稿:網(wǎng)友
java讀取并導(dǎo)出多類型數(shù)據(jù)csv文件

1、主函數(shù)

public class csvtest { public static void main(String[] args) {

List<Car> dataList = CSVUtils.importCsv(new File("D:/test.csv"));

boolean isSuccess = CSVUtils.exportCsv(new File("D:/1.csv"), dataList); System.out.PRintln(isSuccess);

}

}

2、Car類

public class Car { String id; double utc; double x; double y; float speed; float angle; double status; public Car(String id, double utc, double x, double y, float speed, float angle, double status) { this.id = id; this.utc = utc; this.x = x; this.y = y; this.speed = speed; this.angle = angle; this.status = status; } public String getId() { return id; } public void setId(String id) { this.id = id; } public double getUtc() { return utc; } public void setUtc(double utc) { this.utc = utc; } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } public float getSpeed() { return speed; } public void setSpeed(float speed) { this.speed = speed; } public float getAngle() { return angle; } public void setAngle(float angle) { this.angle = angle; } public double getStatus() { return status; } public void setStatus(double status) { this.status = status; } }3、讀取和導(dǎo)出工具類public class CSVUtils { public static boolean exportCsv(File file, List<Car> dataList) { boolean isSucess = false; FileOutputStream out = null; OutputStreamWriter osw = null; BufferedWriter bw = null; try { out = new FileOutputStream(file); osw = new OutputStreamWriter(out); bw = new BufferedWriter(osw); if (dataList != null && !dataList.isEmpty()) { for (int i = 0; i < dataList.size(); i++) { String[] temp = new String[7]; StringBuffer data = new StringBuffer(); Car car = dataList.get(i); temp[0] = car.id; temp[1] = String.valueOf(car.utc); temp[2] = String.valueOf(car.x); temp[3] = String.valueOf(car.y); temp[4] = String.valueOf(car.speed); temp[5] = String.valueOf(car.angle); temp[6] = String.valueOf(car.status); for (int j = 0; j < 6; j++) { data.append(temp[j] + ","); } data.append(temp[6]); //String data=temp[0]+","+temp[1]+","+temp[2]+","+temp[3]+","+temp[4]+","+temp[5]+","+temp[6]; bw.append(data).append("/r"); } } isSucess = true; } catch (Exception e) { isSucess = false; } finally { if (bw != null) { try { bw.close(); bw = null; } catch (IOException e) { e.printStackTrace(); } } if (osw != null) { try { osw.close(); osw = null; } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); out = null; } catch (IOException e) { e.printStackTrace(); } } } return isSucess; } public static List<Car> importCsv(File file) { List<Car> dataList = new ArrayList<Car>(); BufferedReader br = null; try { br = new BufferedReader(new FileReader(file)); String line; while ((line = br.readLine()) != null) { String[] temp = line.split(","); String id = temp[0]; double utc = Double.parseDouble(temp[1]); double x = Double.parseDouble(temp[2]); double y = Double.parseDouble(temp[3]); float speed = Float.parseFloat(temp[4]); float angle = Float.parseFloat(temp[5]); double status = Double.parseDouble(temp[6]); Car car = new Car(id, utc, x, y, speed, angle, status); dataList.add(car); } } catch (Exception e) { } finally { if (br != null) { try { br.close(); br = null; } catch (IOException e) { e.printStackTrace(); } } } return dataList; }}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 武定县| 巴东县| 嵊泗县| 天祝| 惠来县| 乌兰浩特市| 司法| 平邑县| 宁强县| 翁源县| 平安县| 洪江市| 嘉定区| 华蓥市| 桐城市| 龙山县| 攀枝花市| 扎兰屯市| 永年县| 兖州市| 昌江| 拜城县| 金堂县| 罗平县| 八宿县| 博湖县| 长葛市| 清丰县| 宁河县| 乌兰县| 岱山县| 天峻县| 遂川县| 兰坪| 淳化县| 宁都县| 陇西县| 高平市| 辉县市| 罗山县| 舞钢市|