Contents
Two ways to do this,
- Using core Java
- Using OpenCSV
Let’s start with the core Java way.
Using core Java
Write content to the CSV file.
public void writeContentToCSVFile() throws IOException { List<List<String>> rows = Arrays.asList( Arrays.asList("Learn Node.js", "Andrew", "programming Tutorial"), Arrays.asList("Learn Scala", "Andrew", "programming Tutorial") ); FileWriter csvWriter = new FileWriter("book.csv"); csvWriter.append("Name"); csvWriter.append(","); csvWriter.append("Author"); csvWriter.append(","); csvWriter.append("Category"); csvWriter.append("\n"); for (List<String> rowData : rows) { csvWriter.append(String.join(",", rowData)); csvWriter.append("\n"); } csvWriter.close(); }
Output
Read content from the CSV file
public void readContentFromCSVFile() throws IOException { try (BufferedReader input = new BufferedReader(new FileReader("book.csv"))) { String row; while ((row = input.readLine()) != null) { String[] data = row.split(","); for (int i = 0; i < data.length; i++) { String comma = (i + 1 == data.length ? "" : ","); System.out.print(data[i] + comma); } System.out.println(); } } }
Output
Name,Author,Category Learn Node.js,Andrew,programming Tutorial Learn Scala,Andrew,programming Tutorial
Using OpenCSV
<dependency> <groupId>com.opencsv</groupId> <artifactId>opencsv</artifactId> <version>4.6</version> </dependency>
package csvdemo; import com.opencsv.CSVReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class OpenCsvDemo { public static void main(String[] args) throws IOException { CSVReader csvReader = new CSVReader(new FileReader("book.csv")); List<String[]> list = new ArrayList<>(); String[] line; while ((line = csvReader.readNext()) != null) { for (int i = 0; i < line.length; i++) { String comma = (i + 1 == line.length ? "" : ","); System.out.print(line[i] + comma); } System.out.println(); list.add(line); } csvReader.close(); } }
Write content to CSV file Using OpenCSV
public void writeToCsvFile() throws IOException { try ( Writer writer = Files.newBufferedWriter(Paths.get("contact.csv")); CSVWriter csvWriter = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER, CSVWriter.DEFAULT_LINE_END); ) { String[] headerRecord = {"Name", "Email", "Phone", "Country"}; csvWriter.writeNext(headerRecord); csvWriter.writeNext(new String[]{"Sundar Pichai ♥", "sundar.pichai@gmail.com", "+1-1111111111", "India"}); csvWriter.writeNext(new String[]{"Satya Nadella", "satya.nadella@outlook.com", "+1-1111111112", "India"}); } }
Output
Name,Email,Phone,Country Sundar Pichai ♥,sundar.pichai@gmail.com,+1-1111111111,India Satya Nadella,satya.nadella@outlook.com,+1-1111111112,India
Read content from CSV file Using OpenCSV
public void readFromCsvFile() throws IOException { CSVReader csvReader = new CSVReader(new FileReader("contact.csv")); List<String[]> list = new ArrayList<>(); String[] line; while ((line = csvReader.readNext()) != null) { for (int i = 0; i < line.length; i++) { String comma = (i + 1 == line.length ? "" : ","); System.out.print(line[i] + comma); } System.out.println(); list.add(line); } csvReader.close(); }