从文本文件Java中提取数据表

时间:2022-02-08 03:45:20

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.

我打算让一个人类具有适当的变量来存储数据,如名字,姓氏,标记和年龄。

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.

但是,当我尝试逐行提取以获取数据行并使用string.split()来分解字符串以获取列时。然后我无法确定哪个数据属于哪个列。

line = br.readLine()

will return "Charlie Brown 5 170cm"

将返回“查理布朗5 170厘米”

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.

和value = line.split(“// s +”);将返回值[查理,布朗,5,170厘米]。此时我无法确定哪个值属于哪个列。

Please help

2 个解决方案

#1


0  

After you extract the line, for each line You can find the index of \t using indexOf method and then use subString to extract part.

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

#2


0  

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.

如果没有任何内容,这将把字符串放在正确的行和空字符串中。

#1


0  

After you extract the line, for each line You can find the index of \t using indexOf method and then use subString to extract part.

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

#2


0  

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.

如果没有任何内容,这将把字符串放在正确的行和空字符串中。