Parsing CSV data

17 11 2008

Parsing CSV data: Its a simple file with values separated by coma(“,”) or pipe(“|”). It can be with .txt or .csv as the extension of the file. If the file is with .txt you can just read the file as normal. Even .csv is a plain text file with coma separated values, some times it may show junk values.

For this you just convert the file name as filename.txt

Here is the code for that.

File file = new File(“csvFile.txt”);
FileInputStream fileStream = new FileInputStream(file);
InputStreamReader inputStream = new InputStreamReader(fileStream);
BufferedReader buffer = new BufferedReader(inputStream);
String line = “”;
while ((line = buffer.readLine()) != null) {
StringTokenizer token = new StringTokenizer(line, “,”);
ProductDataFeeding dataFeeding = new ProductDataFeeding();
while (token.hasMoreTokens()) {
System.out.println(“value====” + token.nextToken());
}
}

If the file is .csv

File file = new File(“csvFile.csv”);

file.renameTo(new File(“textFile.txt”));

and the remaining code is same.








Follow

Get every new post delivered to your Inbox.