java - Issue while reading tab delimited text file? -
i have tab delimited file , have read data file.
col1 col2 col3 data1 data2 data3 data1 data2 data3
if values present columns no isssues. problem few columns may not contain values below.
col1 col2 col3 data1 data3 data1 data2
in above data, able read first rows data col2's value empty string. second row's col3 has no data. here array index out of bounds exception. why not getting empty string second row's col3?
i using code below:
string datafilename = "c:\\documents , settings\\user1\\some.txt"; /** * creating buffered reader read file */ bufferedreader breader = new bufferedreader( new filereader(datafilename)); string line; /** * looping read block until lines in file read. */ while ((line = breader.readline()) != null) { /** * splitting content of tabbed separated line */ string datavalue[] = line.split("\t"); string value1 = datavalue[0]; string value2 = datavalue[1]; string value3 = datavalue[2]; }
thanks!
the lazy way like:
... string datavalue[] = arrays.copyof(line.split("\t"),3); string value1 = datavalue[0]; string value2 = datavalue[1]; string value3 = datavalue[2]; ...
basically splitting content , copying new array in padded elements null documented:
copies specified array, truncating or padding nulls (if necessary) copy has specified length. indices valid in both original array , copy, 2 arrays contain identical values. indices valid in copy not original, copy contain null. such indices exist if , if specified length greater of original array. resulting array of same class original array.
Comments
Post a Comment