Java Tutorial
Reads the CSV file and returns each row as line and column values are separated with ' ' single space.
Let us consider test.csv file contains the below rows.
id,name,class 1,student1, 10 2,student2, 9
This java program parses the CSV file and provides the customized string content for each csv file lines.
import java.io.*; public class CSVReaderTest { public static void readCSVFile(String csvFile, String delimiter) { try { File file = new File(csvFile); BufferedReader br = new BufferedReader(new FileReader(file)); String line = ""; String[] columnsStr; while ((line = br.readLine()) != null) { if(line.indexOf("id")>=0){ continue; } columnsStr = line.split(delimiter); for (int i=0;i<columnsStr.length;i++){ System.out.print(columnsStr[i] + " "); } System.out.println(); } br.close(); } catch (IOException ex) { System.out.println("IOException: "+ex.toString()); } } public static void main(String[] args) { // csv file to read String csvFile = "d:/test.csv"; String delim = ","; CSVReaderTest.readCSVFile(csvFile, delim); } }Output:
D:\Java_Programs>javac CSVReaderTest.java D:\Java_Programs>java CSVReaderTest 1 student1 10 2 student2 9
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page