-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create sniper for Read and CSV file using Apache Commons CSV
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-csv</artifactId> <version>1.5</version> </dependency>
- Loading branch information
1 parent
9f937b9
commit 1dee2b3
Showing
95 changed files
with
337 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Name,Email,Phone,Address | ||
kibria,[email protected],01431921892,dhaka bangladesh | ||
anika,[email protected],01631921892,Naogaon |
93 changes: 93 additions & 0 deletions
93
src/main/java/com/CrackCode/ApachePoi/CSVfileReadWrite/CSVReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.CrackCode.ApachePoi.CSVfileReadWrite; | ||
|
||
import org.apache.commons.csv.CSVFormat; | ||
import org.apache.commons.csv.CSVParser; | ||
import org.apache.commons.csv.CSVRecord; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
/* | ||
@TODO Need this dependency | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-csv</artifactId> | ||
<version>1.5</version> | ||
</dependency> | ||
*/ | ||
public class CSVReader { | ||
|
||
private static final String SAMPLE_CSV_FILE_PATH = "./sample.csv";//--expected Readable CSV File Name | ||
|
||
public static void main(String[] args) throws IOException { | ||
/** | ||
* @TODO Read and print CSV data with Headers to console | ||
*/ | ||
//here get Data From Csv File using valid file name | ||
CSVParser csvDataWithHeaders = new CSVReader().getCsvDataListWithHeaders(SAMPLE_CSV_FILE_PATH); | ||
//Now print | ||
printCSVdata(csvDataWithHeaders); | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
System.out.println("\n\n\n\n\n\n\n\n\n-----------------------------Now Read Data Without Headers----------------------------------------\n\n\n\n\n\n\n\n\n\n\n"); | ||
|
||
|
||
|
||
|
||
|
||
/** | ||
* @TODO Read and print CSV data without Headers to console | ||
*/ | ||
//here get Data From Csv File using valid file name | ||
CSVParser csvDataWithoutHeaders = new CSVReader().getCsvDataListWithoutHeaders(SAMPLE_CSV_FILE_PATH); | ||
//Now print | ||
printCSVdata(csvDataWithoutHeaders); | ||
|
||
} | ||
|
||
public CSVParser getCsvDataListWithHeaders(String fileName) throws IOException { | ||
Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH)); | ||
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT | ||
.withHeader("Name", "Email", "Phone", "Address")//those are headers name. | ||
.withIgnoreHeaderCase() | ||
.withTrim()); | ||
return csvParser; | ||
} | ||
|
||
public CSVParser getCsvDataListWithoutHeaders(String fileName) throws IOException { | ||
Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH)); | ||
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT | ||
.withFirstRecordAsHeader() | ||
.withIgnoreHeaderCase() | ||
.withTrim()); | ||
return csvParser; | ||
} | ||
|
||
|
||
|
||
//only for print data | ||
public static void printCSVdata(CSVParser csvRecords){ | ||
for (CSVRecord csvRecord :csvRecords ) { | ||
// Accessing values by the names assigned to each column | ||
String name = csvRecord.get("Name"); | ||
String email = csvRecord.get("Email"); | ||
String phone = csvRecord.get("Phone"); | ||
String address = csvRecord.get("Address"); | ||
|
||
System.out.println("Record No - " + csvRecord.getRecordNumber()); | ||
System.out.println("---------------"); | ||
System.out.println("Name : " + name); | ||
System.out.println("Email : " + email); | ||
System.out.println("Phone : " + phone); | ||
System.out.println("Address : " + address); | ||
System.out.println("---------------\n\n"); | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/CrackCode/ApachePoi/CSVfileReadWrite/CSVWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.CrackCode.ApachePoi.CSVfileReadWrite; | ||
|
||
import com.CrackCode.designPattern.Prototype.Student; | ||
import org.apache.commons.csv.CSVFormat; | ||
import org.apache.commons.csv.CSVPrinter; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/* | ||
@TODO Need this dependency | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-csv</artifactId> | ||
<version>1.5</version> | ||
</dependency> | ||
*/ | ||
public class CSVWriter { | ||
private static final String SAMPLE_CSV_FILE = "./sample.csv";//--expected CSV File Name | ||
|
||
public static void main(String[] args) throws IOException { | ||
String[] headers = {"Name", "Email", "Phone","Address"};//for headers | ||
//now prepare data | ||
List<Student> dataList = new ArrayList<>(); | ||
dataList.add(new Student("kibria","[email protected]","01431921892","dhaka bangladesh")); | ||
dataList.add(new Student("anika","[email protected]","01631921892","Naogaon")); | ||
//Now send data to write CSV File | ||
new CSVWriter().writeDataTCsv(SAMPLE_CSV_FILE,headers, dataList); | ||
} | ||
|
||
|
||
/** | ||
* @TODO Method For Write Data to CSV file | ||
* @param fileName | ||
* @param headers | ||
* @param rowsDataList | ||
* @throws IOException | ||
*/ | ||
public void writeDataTCsv(String fileName, String[] headers,List<Student> rowsDataList) throws IOException { | ||
try { | ||
BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName)); | ||
|
||
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT | ||
.withHeader(headers/*Add headers here*/)); | ||
|
||
//now set data to print | ||
rowsDataList.forEach(student -> { | ||
try { | ||
csvPrinter.printRecord(student.getName(), student.getEmail(), student.getPhone(), student.getAddress()); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
csvPrinter.flush(); //Finally, flash the operation | ||
|
||
}catch (Exception e){ | ||
throw e; | ||
} | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...estion/ApachePoi/ContentReadListener.java → .../XLfileReadWrite/ContentReadListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ewQuestion/ApachePoi/ExcelFileReader.java → ...ePoi/XLfileReadWrite/ExcelFileReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ewQuestion/ApachePoi/ExcelFileWriter.java → ...ePoi/XLfileReadWrite/ExcelFileWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...Question/ApachePoi/SpreadSheetReader.java → ...oi/XLfileReadWrite/SpreadSheetReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...Question/ApachePoi/SpreadSheetWriter.java → ...oi/XLfileReadWrite/SpreadSheetWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...hePoi/XLReadWriteTest/ReadFromXlTest.java → ...Write/XLReadWriteTest/ReadFromXlTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ackCode/interviewQuestion/BeanConfig.java → src/main/java/com/CrackCode/BeanConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...uestion/InterviewQuestionApplication.java → ...ackCode/InterviewQuestionApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ion/Polymorphism/example1/Programmer.java → ...ode/Polymorphism/example1/Programmer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../Polymorphism/example1/RuntimeResult.java → .../Polymorphism/example1/RuntimeResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...uestion/Polymorphism/example1/Tester.java → ...ackCode/Polymorphism/example1/Tester.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../Polymorphism/example2/Communication.java → .../Polymorphism/example2/Communication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ewQuestion/Polymorphism/example2/Man.java → .../CrackCode/Polymorphism/example2/Man.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../Polymorphism/example2/RuntimeOutput.java → .../Polymorphism/example2/RuntimeOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...Question/Polymorphism/example2/Woman.java → ...rackCode/Polymorphism/example2/Woman.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
.../interviewQuestion/Reflection/Person.java → ...java/com/CrackCode/Reflection/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.CrackCode.interviewQuestion.Reflection; | ||
package com.CrackCode.Reflection; | ||
|
||
|
||
|
||
|
2 changes: 1 addition & 1 deletion
2
...ewQuestion/Reflection/ReflectionTest.java → .../CrackCode/Reflection/ReflectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...nDeserialization/FullExample/Student.java → ...nDeserialization/FullExample/Student.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...erialization/FullExample/StudentMain.java → ...erialization/FullExample/StudentMain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...izationDeserialization/Serialization.java → ...izationDeserialization/Serialization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...com/CrackCode/interviewQuestion/Test.java → src/main/java/com/CrackCode/Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...terviewQuestion/collection/Frequency.java → ...a/com/CrackCode/collection/Frequency.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...tion/database/INNER_JOIN/model/Child.java → ...Code/database/INNER_JOIN/model/Child.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ion/database/INNER_JOIN/model/Parent.java → ...ode/database/INNER_JOIN/model/Parent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...NNER_JOIN/repository/ChildRepository.java → ...NNER_JOIN/repository/ChildRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...NER_JOIN/repository/ParentRepository.java → ...NER_JOIN/repository/ParentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
...wQuestion/database/INNER_JOIN_WORKER.java → ...CrackCode/database/INNER_JOIN_WORKER.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.