Skip to content

Commit 944f850

Browse files
committed
Added CSV File Parse
1 parent f5afa80 commit 944f850

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/src/TestProgram.java
22
/src/TestProgram2.java
3+
/src/TestProgram3.java

sample_file.csv

+1
Large diffs are not rendered by default.

src/JavaParseCsvFile.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import org.apache.commons.csv.CSVFormat;
2+
import org.apache.commons.csv.CSVParser;
3+
import org.apache.commons.csv.CSVRecord;
4+
5+
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.InputStreamReader;
8+
9+
public class JavaParseCsvFile {
10+
public static void main(String args[]){
11+
System.out.println("=============Java CSV Parse Example================");
12+
13+
File csvFile=new File("sample_file.csv");
14+
15+
try {
16+
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(csvFile));
17+
CSVParser csvParser= CSVFormat.DEFAULT.parse(inputStreamReader);
18+
for (CSVRecord csvRecord:csvParser){
19+
System.out.println("Column 1 : "+csvRecord.get(0)+" | Column 2 : "+csvRecord.get(1)+"| Column 3 : "+csvRecord.get(2));
20+
}
21+
22+
}
23+
catch (Exception e){
24+
System.out.println("Error in Parsing CSV File");
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)