What is CSV?
CSV is comma-separated values.It is a type of file format. Here the data are
separated by the delimiter comma(,).
What is our Aim?
Sometimes we need to scrape data from different files.Csv on of the popular
file. Today We will see how to open a CSV file and parse the file and arrange
the data in our format. The language we used in this post Java.
Let's get Started 😀
There are two ways to do our task.
- Using hardcoded logic
- Using a library named opencsv
1. Let's build a logic :
First, we have to open the file. For that, we can use BufferReader.
Second, we know the key point regarding CSV that each line of the file
consists of one or more fields, separated by commas. So here we can split the
line by the delimiter comma(,) and store them in a list or array.
Last but not least we have to access all row's data with the help of the index
stored in the list.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class sample {
public static void main(String[] args) throws IOException {
BufferedReader br=null;
try {
br=new BufferedReader(new FileReader("sample.csv"));
String line;
String[] attributesHeader;
int count=0;
List<String>list=new ArrayList<String>();
while((line=br.readLine())!=null) {
if(line.contains("Name")) {
attributesHeader=line.split(",");
for(String header:attributesHeader) {
list.add(header.replace(" ", "").toUpperCase());
}
count++;
System.out.println("HEADERLIST : "+list);
}
else if(line.contains("Total Students"))
{
count--;
continue;
}
else if(count>=1) {
String[] attributes=line.split(",");
System.out.print(attributes[list.indexOf("NAME")]+"\t");
System.out.print(attributes[list.indexOf("BRANCH")]+"\t\t");
System.out.print(attributes[list.indexOf("ROLLNO")]+"\t\t");
System.out.print(attributes[list.indexOf("COLLEGE")]+"\t\t");
System.out.print(attributes[list.indexOf("RANK")]+"\t\t");
}
System.out.println();
}
}catch (IOException e){
e.printStackTrace();
}
finally {
br.close();
}
}
}
OUTPUT IN CONSOLE :
2. Using a library named OPENCSV
Opencsv is an easy-to-use CSV (comma-separated values) parser library for
Java. To use Opencsv you can download the jar files from this
link or you can below code in your maven dependency
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.3</version>
</dependency>
CsvReader will do our work in just one line. Let's see the code
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import au.com.bytecode.opencsv.CSVReader;
public class sample1 {
public static void main(String[] args) throws IOException {
CSVReader csvReader=null;
try{
csvReader= new CSVReader(new FileReader("sample.csv"));
String[] nextRecord,header;
header = csvReader.readNext();
List<String> list=new ArrayList<String>();
for(String headercell:header)
{
list.add(headercell.replace(" ", "").toUpperCase());
}
System.out.println("Headerlist : "+list);
while ((nextRecord = csvReader.readNext()) != null) {
System.out.print(nextRecord[list.indexOf("NAME")]+"\t");
System.out.print(nextRecord[list.indexOf("BRANCH")]+"\t");
System.out.print(nextRecord[list.indexOf("ROLLNO")]+"\t");
System.out.print(nextRecord[list.indexOf("COLLEGE")]+"\t");
System.out.print(nextRecord[list.indexOf("RANK")]+"\t");
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
csvReader.close();
}
}
}
OUTPUT IN CONSOLE :
Happy Coding!!
1 Comments
how to parse pdf file containing headerlevel data and item level data in java using regex ?
ReplyDeletePost a Comment
You are welcome to share your thoughts with us!