第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > java读取并导出多类型数据csv文件

java读取并导出多类型数据csv文件

时间:2020-07-26 21:24:09

相关推荐

java读取并导出多类型数据csv文件

1、主函数

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、读取和导出工具类

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;

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。