[JAVA] Android CSV library

Java library I thought I could use anything on Android. It wasn't, so I'll summarize it.

Open CSV

Decompose with commas to make an array of character strings and get them in the order.

http://opencsv.sourceforge.net/

Sample program here https://github.com/ohwada/Android_Samples/tree/master/Csv1

A file with a header line like the one below Also has the ability to map to Java objects.

"Name","Quantity"
"Apple", "10"
"Banana", "20"

However, on Android, the following error occurs.

NoClassDefFoundError: Failed resolution of: Ljava/beans/Introspector

Android doesn't seem to have the required libraries.

Reference: [Didn't find class “java.beans.Introspector” when Using OpenCSV to parse csv files](https://stackoverflow.com/questions/50173340/didnt-find-class-java-beans-introspector-when- using-opencsv-to-parse-csv-file)

univocity-parsers

Has the ability to map CSV files to Java objects.

https://www.univocity.com/pages/univocity_parsers_tutorial

How to use

Prepare a Java class that matches the columns of the CSV file. Specify the correspondence between columns and variables with annotations.

@Data
public class Hoge {

@Parsed(field = "Name")
public String name = "";

@Parsed(field = "Quantity")
public int quantity = 0;

Read CSV file

    //When reading from the Asset folder
    Reader reader = new InputStreamReader( getAssets().open(file name) );

        CsvParserSettings settings = new CsvParserSettings();
    BeanListProcessor<Shopping> rowProcessor = new BeanListProcessor<>(Hoge.class);
        settings.setProcessor(rowProcessor);

 CsvRoutines routines = new CsvRoutines(settings);
List<Hoge> list
 = routines.parseAll( Hoge.class,  reader );

Sample program here https://github.com/ohwada/Android_Samples/tree/master/Csv2

Apache Commons CSV

Decompose with commas to make an array of character strings and get them in the order.

https://commons.apache.org/proper/commons-csv/

Sample program here https://github.com/ohwada/Android_Samples/tree/master/Csv3

Recommended Posts

Android CSV library
Android SIP Library
Java CSV library "opencsv"
Multipart transmission library on Android
Android library collection of "Mercariatte"
Library collection useful for Android development
Android view
3 ways to import the library in Android Studio