Java reads and exports csv files of multiple types of data, and java reads and exports csv files
1. Main Function
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. Read and export tool
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;
}
}