简体   繁体   中英

Extract data table from text file Java

I have a problem of extracting table of data from text file using java.

The table is arrange as below:

FirstName Surname Mark Age Height

Charlie   Brown   5         170cm
Lucy      Harlow      16    160cm
Jame      Horde   11  18
Charrlot  White       19    165cm
Jimmy     Lutton  15  17    180cm

I intended to have a person class which has appropriate variables to stored data such as first name, surname, mark and age.

However when I tried to extract the line by line to get the data line and use string.split() to break down the string to get the column. Then I cannot determine the which data belong to which column.

line = br.readLine()

will return "Charlie Brown 5 170cm"

and value = line.split("//s+"); will return value[Charlie,Brown,5,170cm] . At this point I cannot determine which value is belong to which column.

Please help

提取行之后,对于每一行,您可以使用indexOf方法找到\\t的索引,然后使用subString提取部分。

If you file is tab separated then you should just split on the tab characters instead of 1 or more white-space characters.

value = line.split("\t");

This will put the strings in the correct rows and empty strings if there is nothing there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM