Reading from PSV file Java -


i want read psv file using java. records in psv file has 4 columns. want read , output 3 , 4th column. best way this. here have:

bufferedreader psvfile = new bufferedreader(new filereader(filename)); string datarow = psvfile.readline(); while (datarow != null) {     string[] dataarray = datarow.split("\n");     (string item:dataarray)      {         string[] elements = item.split("|");         system.out.println(item);     }     system.out.println();     datarow = psvfile.readline(); } psvfile.close(); system.out.println(); 

based on @aljoshabre suggestion iam using csvreader, doing this:

reader = new csvreader(new filereader(filename),'|'); string [] nextline; while ((nextline = reader.readnext()) != null)  {  system.out.println( nextline[3] + nextline[4]); } 

i getting desired output error: exception in thread "main" java.lang.arrayindexoutofboundsexception: 2 @ read_psv.main(read_psv.java:20) line 20 system.out.println( nextline[3] + nextline[4]);

opencsv weapon of choice.

this snippet third , forth columns:

try {     //last parameter tells line (row) consider first 1     csvreader reader = new csvreader(new filereader("res/test.csv"), '|', '\0', 1);     string[] row;      list<string> columnthree    = new arraylist<string>();     list<string> columnfour     = new arraylist<string>();      while((row = reader.readnext()) != null) {         columnthree.add(row[2]);         columnfour.add(row[3]);     }     reader.close(); } catch (filenotfoundexception e) {     e.printstacktrace(); } catch (ioexception e) {     e.printstacktrace(); } 

it print

nikola nenad ljubinka gordana 

for input:

test.psv

name|surname|father|mother aleksandar|milic|nikola|ljubinka nebojsa|jakovljevic|nenad|gordana 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -