public class CSVParser

  1. Object
  2. CSVParser

The CSVParser allows importing data into applications quickly from a CSV source. CSV is a file format that is supported by all spreadsheets and most databases for data exchange.

The sample below lists a simple hardcoded CSV within the table:

Form hi = new Form("CSV Parsing", new BorderLayout());
CSVParser parser = new CSVParser();
try(Reader r = new com.codename1.io.CharArrayReader("1997,Ford,E350,\"Super, \"\"luxurious\"\" truck\"".toCharArray())) {
    String[][] data = parser.parse(r);
    String[] columnNames = new String[data[0].length];
    for(int iter=  0 ; iter

@author Shai Almog

Constructors

public CSVParser()Initializes a parser with the default comma (’,’) separator char
public CSVParser(char separatorChar)Allows creating a parser with a custom separator char

Methods

public String[][] parse(InputStream r) throws IOExceptionParses input from the given stream and returns the tokens broken into rows and columns
public String[][] parse(InputStream r, String encoding) throws IOExceptionParses input from the given stream and returns the tokens broken into rows and columns
public String[][] parse(Reader r) throws IOExceptionParses input from the given reader and returns the tokens broken into rows and columns

Inherited methods

Constructor details

CSVParser

public CSVParser()
Initializes a parser with the default comma (’,’) separator char

CSVParser

public CSVParser(char separatorChar)
Allows creating a parser with a custom separator char

Parameters

separatorChar char
custom separator character such as semi-colon (’;’) etc.

Method details

parse

public String[][] parse(InputStream r) throws IOException
Parses input from the given stream and returns the tokens broken into rows and columns

Parameters

r InputStream
the input stream

Returns

array of rows and columns

parse

public String[][] parse(InputStream r, String encoding) throws IOException
Parses input from the given stream and returns the tokens broken into rows and columns

Parameters

r InputStream
the input stream
encoding String
the encoding of the stream

Returns

array of rows and columns

parse

public String[][] parse(Reader r) throws IOException
Parses input from the given reader and returns the tokens broken into rows and columns

Parameters

r Reader
the reader stream

Returns

array of rows and columns