java - handling of different delimiters of a csv file -
i new java , know basics now. have csv file lines of following structure:
int,,text,text,int,text,text,text,,text,text,,text,text,,,text,,text,,,int,int
i confused when saw csv file since separated single commas, double commas , triple commas. specific text or int empty , excel can´t handle display csv in correct way more.
so thought use java write program make columns separated 1 comma. , save result in new csv file afterwards. (i haven´t implemented how write in file) research managed write file reader read csv file that´s it. how can come desired result?
what have done far:
import java.io.bufferedreader; import java.io.filereader; import java.io.ioexception; import java.util.arraylist; import java.util.arrays; class read { public static void main(string[] args) { filereader myfile = null; bufferedreader buff = null; final arraylist<string> lines = new arraylist<string>(); try { myfile = new filereader("thisisthepathofthecsvsource"); buff = new bufferedreader(myfile); string line; while ((line = buff.readline()) != null) { lines.add(line); } } catch (ioexception e) { system.err.println("error2 :" + e); } { try { buff.close(); myfile.close(); } catch (ioexception e) { system.err.println("error2 :" + e); } } final string[][] valuesarray = new string[lines.size()][]; int cnt = 0; (final string line : lines) { valuesarray[cnt++] = line.split(","); } (string[] arr : valuesarray) { system.out.println(arrays.tostring(arr)); } } }
try open source library univocity-parsers, provides solution of columns separator following:
csvparsersettings settings = new csvparsersettings(); settings.setskipemptylines(true); settings.getformat().setlineseparator("\n"); settings.getformat().setquote(','); settings.getformat().setquoteescape('\\'); // escape double backslash
Comments
Post a Comment