Read and Write CSV as String

Updated: Nov 10, 2023

This example shows how to read and write comma-separated values as a single field using the CommaSeparatedValues class.

Java Code Listing

package com.northconcepts.datapipeline.examples.csv;

import com.northconcepts.datapipeline.csv.CommaSeparatedValues;

public class ReadAndWriteCsvAsString {

    private static final String[] CONTINENTS = {"Africa", "Antarctica", "Asia", "Australia", "Europe", "North America", "South America"};

    public static void main(String[] args) {

        CommaSeparatedValues commaSeparatedValues = CommaSeparatedValues.fromArray(CONTINENTS);


        CommaSeparatedValues csvFromString = CommaSeparatedValues.fromValue(commaSeparatedValues.toCsvString());


Code Walkthrough

  1.  An instance of CommaSeparatedValues is created using .fromArray(CONTINENTS) which takes in the array CONTINENTS as parameter.
  2. .toCsvString() is used to get a  String and printed to console.
  3. .remove(1) is used to remove the value at index 1.
  4. .toCsvString() is used again to print the remaining values to console.
  5. Another instance of CommaSeperataValues is created using  .fromValue(commaSeparatedValues.toCsvString()) which takes in a String as argument.
  6. An overridden .toString() method which calls .toCsvString() is used to print the values to console.
  7. .subList(2,5) is used to get a new instance of CommaSeparatedValues containing only the elements at index 2 to 5. .toCsvString() is then used to print the values to console.


Mobile Analytics