------------------------------------------Example : 1-----------------------------------------
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
class CSVFileReader {
String fileName;
ArrayList <String>storeValues = new ArrayList<String>();
public CSVFileReader(String FileName)
{
this.fileName=FileName;
}
public void ReadFile()
{
try {
//storeValues.clear();//just in case this is the second call of the ReadFile Method./
BufferedReader br = new BufferedReader( new FileReader(fileName));
StringTokenizer st = null;
int lineNumber = 0, tokenNumber = 0;
while( (fileName = br.readLine()) != null)
{
lineNumber++;
System.out.println(fileName);
storeValues.add(fileName);
//break comma separated line using ","
st = new StringTokenizer(fileName, ",");
while(st.hasMoreTokens())
{
tokenNumber++;
System.out.println("Line # " + lineNumber +
", Token # " + tokenNumber
+ ", Token : "+ st.nextToken());
}
//reset token number
tokenNumber = 0;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//mutators and accesors
public void setFileName(String newFileName)
{
this.fileName=newFileName;
}
public String getFileName()
{
return fileName;
}
public ArrayList getFileValues()
{
return this.storeValues;
}
public void displayArrayList()
{
for(int x=0;x<this.storeValues.size();x++)
{
System.out.println(storeValues.get(x));
}
}
}
class MainA {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName="D:/Documents and Settings/Cherry/Desktop/asd.csv";
CSVFileReader x=new CSVFileReader(fileName);
x.ReadFile();
x.displayArrayList();
}
}
CSV : asd.csv
sln,name,address,phonenumber,Discription
1,neeth,mknagar,9492,SE
2,praneeth,Hnmnpt,6455,MCA
3,pradeep,mknagar,7382,MBA
Execution :
D:\Documents and Settings\Cherry\Desktop>javac b.java
D:\Documents and Settings\Cherry\Desktop>java MainA
D:\Documents and Settings\Cherry\Desktop>
------------------------------------------Example : 2-----------------------------------------
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.StringTokenizer;
class Mainq {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName="D:/Documents and Settings/Cherry/Desktop/asd.csv";
try {
BufferedReader br = new BufferedReader( new FileReader(fileName));
String strLine = null;
StringTokenizer st = null;
int lineNumber = 0, tokenNumber = 0;
while( (fileName = br.readLine()) != null)
{
lineNumber++;
//break comma separated line using ","
st = new StringTokenizer(fileName, ",");
while(st.hasMoreTokens())
{
//display csv values
tokenNumber++;
System.out.println("Line # " + lineNumber +
", Token # " + tokenNumber
+ ", Token : "+ st.nextToken());
}
//reset token number
tokenNumber = 0;
}
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Execution :
Comments