java - Whats the simplest way when writing & reading a 2D-boolean-Array to a CSV File? -


i'd write 2-dimensional boolean-array csv file. im using apache commons' csvparser. problem couldnt find built-in way turn array can written csv-file , later converted back.

so there way apart using arrays.deeptostring(...) , writing complicated , error-prone function parse array?

it great if boolean[][] array = arrays.parse<boolean[][]>(arrays.deeptostring(oldarray)) existed...

thanks help.

use univocity-parsers write/read booleans.

public static void main(string ... args){     csvwritersettings writersettings = new csvwritersettings();      objectrowwriterprocessor writerprocessor = new objectrowwriterprocessor(); // handles rows of objects , conversions string.     writerprocessor.convertall(conversions.toboolean("t", "f")); // write "t" , "f" instead of "true" , "false"      writersettings.setrowwriterprocessor(writerprocessor);      csvwriter writer = new csvwriter(writersettings);     writersettings.setheaders("a", "b", "c", "d");      string line1 = writer.processrecordtostring(true, false, false, true);     string line2 = writer.processrecordtostring(false, false, true, true);      system.out.println("### rows written ###");      system.out.println(line1);     system.out.println(line2);      // now, let's read these lines      csvparsersettings parsersettings = new csvparsersettings();      objectrowlistprocessor readerprocessor = new objectrowlistprocessor(); // handles conversions string objects , adds result list     readerprocessor.convertall(conversions.toboolean("t", "f")); //reads "t" , "f" true , false      parsersettings.setrowprocessor(readerprocessor);      csvparser parser =  new csvparser(parsersettings);     parser.parseline(line1); //handled readerprocessor     parser.parseline(line2); //handled readerprocessor      system.out.println("### rows parsed ###");      list<object[]> rows = readerprocessor.getrows();     for(object[] row : rows){         system.out.println(arrays.tostring(row));     } } 

the code above produces:

### rows written ### t,f,f,t f,f,t,t ### rows parsed ### [true, false, false, true] [false, false, true, true] 

note: shouldn't need explicitly set headers when writing, found (and fixed) in master branch of project


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -