Here i would like to share that how to convert a csv file to java object.
Records in sample csv
Professional.java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
*
* @author Jamsheer T
*
*/
public class CSVtoJava {
public void convertCsvToJava() {
Professional profObject = new Professional();
String csvFileToRead = "C:\\Users\\Jamsheer T\\Desktop\\csv\\prof.csv";
BufferedReader br = null;
String line = "";
String splitBy = ",";
boolean initial =true;
try {
br = new BufferedReader(new FileReader(csvFileToRead));
while ((line = br.readLine()) != null) {
// split on comma(',')
String[] details = line.split(splitBy);
// create car object to store values
// add values from csv to car object
profObject.setNpi(details[0]);
profObject.setName(details[1]);
profObject.setMname(details[2]);
profObject.setLname(details[3]);
profObject.setDesig(details[4]);
if(initial==true){
initial=false;
continue;
}
System.out.println("NPI :" + profObject.getNpi());
System.out.println("NAME :" + profObject.getName());
System.out.println("Designation :" + profObject.getDesig());
System.out.println("----------------------------------");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Records in sample csv
NPI
|
Fisrt Name
|
Middle Name
|
Last Name
|
Designation
|
1000
|
Ajmal
|
A
|
Abdullah
|
Software Engineer
|
2000
|
Jouhar
|
O
|
Oravingal
|
Software Engineer
|
3000
|
Eldho
|
J
|
John
|
Software Engineer
|
Professional.java
/**
*
* @author Jamsheer T
*
*/
public class Professional {
String npi;
String name;
String mname;
String lname;
String desig;
public String getNpi() {
return npi;
}
public void setNpi(String npi) {
this.npi = npi;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getDesig() {
return desig;
}
public void setDesig(String desig) {
this.desig = desig;
}
}
CSVtoJava.java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
*
* @author Jamsheer T
*
*/
public class CSVtoJava {
public void convertCsvToJava() {
Professional profObject = new Professional();
String csvFileToRead = "C:\\Users\\Jamsheer T\\Desktop\\csv\\prof.csv";
BufferedReader br = null;
String line = "";
String splitBy = ",";
boolean initial =true;
try {
br = new BufferedReader(new FileReader(csvFileToRead));
while ((line = br.readLine()) != null) {
// split on comma(',')
String[] details = line.split(splitBy);
// create car object to store values
// add values from csv to car object
profObject.setNpi(details[0]);
profObject.setName(details[1]);
profObject.setMname(details[2]);
profObject.setLname(details[3]);
profObject.setDesig(details[4]);
if(initial==true){
initial=false;
continue;
}
System.out.println("NPI :" + profObject.getNpi());
System.out.println("NAME :" + profObject.getName());
System.out.println("Designation :" + profObject.getDesig());
System.out.println("----------------------------------");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Main.java
/**
*
* @author Jamsheer T
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CSVtoJava csvToJavaObject = new CSVtoJava();
csvToJavaObject.convertCsvToJava();
}
}
OUTPUT
NPI :1000
NAME :Ajmal
Designation :Software Engineer
----------------------------------
NPI :2000
NAME :Jouhar
Designation :Software Engineer
----------------------------------
NPI :3000
NAME :Eldho
Designation :Software Engineer
----------------------------------
OUTPUT
NPI :1000
NAME :Ajmal
Designation :Software Engineer
----------------------------------
NPI :2000
NAME :Jouhar
Designation :Software Engineer
----------------------------------
NPI :3000
NAME :Eldho
Designation :Software Engineer
----------------------------------
No comments:
Post a Comment