简体   繁体   English

从文本文件Java提取数据表

[英]Extract data table from text file Java

I have a problem of extracting table of data from text file using java. 我有一个问题,使用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. 我打算创建一个Person类,该类具有适当的变量来存储数据,例如名字,姓氏,标记和年龄。

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. 但是,当我尝试逐行提取以获取数据行并使用string.split()分解字符串以获取列时。 Then I cannot determine the which data belong to which column. 然后,我无法确定哪些数据属于哪一列。

line = br.readLine()

will return "Charlie Brown 5 170cm" 将返回“ Charlie Brown 5 170cm”

and value = line.split("//s+"); value = line.split("//s+"); will return value[Charlie,Brown,5,170cm] . 将返回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. 如果文件是用制表符分隔的,则应仅在制表符上分开而不是1个或多个空格字符。

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

This will put the strings in the correct rows and empty strings if there is nothing there. 这将把字符串放在正确的行中,如果没有任何内容,则将空字符串放入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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